[英]How do I perform a query in Mongoose?
> 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.这就是我的 Mongo object。 Now i want to find it via Mongoose:
现在我想通过 ZCCADDEDB567ABAE643E15DCF0974E503Z 找到它:
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. 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 });
});
I'm one of the authors of mongoose.我是 mongoose 的作者之一。 You can do this query in one of several ways:
您可以通过以下几种方式之一执行此查询:
find
syntax find
语法
User.findOne({'fb.gender': 'male'}, callback);
where
syntax where
语法
User.where('fb.gender', 'male').findOne(callback);
named scope syntax命名为 scope 语法
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);
Try this:尝试这个:
User.findOne( { $where : "this.fb.gender == 'male' " } )
or或者
User.findOne( { fb.gender : "male" } )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.