Matthew Nolan
Matthew Nolan

Reputation: 21

MongoJS Node findOne falsely returns no results

Using MongoJS: https://github.com/mafintosh/mongojs

Finds everything


    db.users.find({}, function(err,users){
        if (err) throw err;
        console.log(users);
    })

Returns the user. looks great


    [{ _id: 53f2faa6aed1689e84982b8b,
        facebook: { 
            email: '[email protected]',
            name: 'Juan Atkins',
            id: '764969936' },
        __v: 0 
    }]

When I try to find that user by his id: failed


    db.users.findOne({
        _id:  '53f2faa6aed1689e84982b8b'
    }, function(err, user) {
        if (err) throw err;
        console.log(user);
    });

returns []

I know there is data in the DB. I've tried searching by a different key (like name). Why can't it find the data?

Upvotes: 2

Views: 534

Answers (1)

Jan Remunda
Jan Remunda

Reputation: 7930

you have to use ObjectId: http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html

db.users.findOne({
    _id:  new ObjectID('53f2faa6aed1689e84982b8b')
}, function(err, user) {
    if (err) throw err;
    console.log(user);
});

Upvotes: 3

Related Questions