> db.users.findOne();
{
"_id" : ObjectId("4db8ebb4c693ec0363000001"),
"fb" : {
"name" : {
"last" : "Sss",
"first" : "Fss",
"full" : "Fss"
},
"updatedTime" : "2011-04-27T09:51:01+0000",
"verified" : true,
"locale" : "en_US",
"timezone" : "-7",
"email" : "[email protected]",
"gender" : "male",
"alias" : "abc",
"id" : "17447214"
}
}
So that's my Mongo object. Now i want to find it via Mongoose:
User.findOne( { gender: "male" }, function(err, docs){
console.log(err); //returns Null
console.log(docs); //returns Null.
});
That doesn't work! Neither does this:
User.findOne( { fb: {gender:"male"} }, function...
Null, null.
This is my entire thing:
app.get('/:uid',function(req,res){
params = {}
User.findOne({ $where : "this.fb.gender == 'male' " }, function(err, docs){
console.log(docs);
});
res.render('user', { locals:params });
});
The Mongoose Query class provides a chaining interface for finding, updating, and deleting documents.
It just means that your new document will have a field with "done" as key and "false" boolean as value.
exec() function returns a promise, that you can use it with then() or async/await to execute a query on a model "asynchronous".
I'm one of the authors of mongoose. You can do this query in one of several ways:
find
syntax
User.findOne({'fb.gender': 'male'}, callback);
where
syntax
User.where('fb.gender', 'male').findOne(callback);
named scope syntax
UserSchema.namedscope('male').where('fb.gender', 'male');
// ...
var User = mongoose.model('User', UserSchema);
// Now you can write queries even more succinctly and idiomatically
User.male.findOne(callback);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With