如何在 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" : "[email protected]",
"gender" : "male",
"alias" : "abc",
"id" : "17447214"
}
}
这就是我的 Mongo 对象。现在我想通过 Mongoose 找到它:
User.findOne( { gender: "male" }, function(err, docs){
console.log(err); //returns Null
console.log(docs); //returns Null.
});
那不起作用!这也不是:
User.findOne( { fb: {gender:"male"} }, function...
空,空。
这就是我的全部事情:
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 });
});
> 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 });
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是猫鼬的作者之一。您可以通过以下几种方式之一执行此查询:
find
语法where
语法命名范围语法
I'm one of the authors of mongoose. You can do this query in one of several ways:
find
syntaxwhere
syntaxnamed scope syntax
试试这个:
或者
Try this :
or