如何在猫鼬中执行查询? [英] How do I perform a query in Mongoose?

查看:55
本文介绍了如何在猫鼬中执行查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> 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"
    }
}

那是我的Mongo对象.现在我想通过猫鼬找到它:

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.

Null, null.

这是我的全部:

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 });
});

推荐答案

尝试一下:

User.findOne( { $where : "this.fb.gender == 'male' " } )

User.findOne( { fb.gender : "male" } )

这篇关于如何在猫鼬中执行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆