> 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" : "abc@gmail.com",
"gender" : "male",
"alias" : "abc",
"id" : "17447214"
}
}
So that's my Mongo object. Now i want to find it via Mongoose:
这就是我的Mongo对象。现在我想通过猫鼬找到它:
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 });
});
2 个解决方案
#1
7
Try this :
试试这个:
User.findOne( { $where : "this.fb.gender == 'male' " } )
or
或
User.findOne( { fb.gender : "male" } )
#2
44
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);
#1
7
Try this :
试试这个:
User.findOne( { $where : "this.fb.gender == 'male' " } )
or
或
User.findOne( { fb.gender : "male" } )
#2
44
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);