Mongoose查找数组中的字段无法正常工作

时间:2014-03-24 19:08:33

标签: node.js mongodb mongoose

我在Node.js和mongodb中运行的应用程序以mongoose作为驱动程序。我创建了一个登录功能,仅用于调试目的,我在查询后打印出文档。这是我的代码:

mongodb的:

{
    "_id" : ObjectId("532cec102e42bcfced71fe03"),
    "admins" : [ 
        {
            "username" : "matthew",
            "name" : "lol",
            "hidden" : {
                "password" : "f8d822f6fab9dfc3c7b3a5035cdc422aee3bb838",
                "access" : true
            }
        }, 
        {
            "username" : "matthew1",
            "name" : "2000",
            "hidden" : {
                "password" : "40bd001563085fc35165329ea1ff5c5ecbdbbeef",
                "access" : false
            }
        }
    ],
    "location" : "SZ",
    "hidden" : {
        "global_access" : true
    },
    "token" : "f10e2821bbbea527ea02200352313bc059445190"
},
{
    "_id" : ObjectId("53307831a6b6ecd28c5cacd7"),
    "admins" : [ 
        {
            "username" : "matthew123123",
            "name" : "lol",
            "hidden" : {
                "password" : "f8d822f6fab9dfc3c7b3a5035cdc422aee3bb838",
                "access" : true
            }
        }, 
        {
            "username" : "matthew1222222",
            "name" : "2000",
            "hidden" : {
                "password" : "40bd001563085fc35165329ea1ff5c5ecbdbbeef",
                "access" : false
            }
        }
    ],
    "location" : "SH",
    "hidden" : {
        "global_access" : true
    },
    "token" : "f10e2821bbbea527ea02200352313bc159445190"
}

branches.js

 var mongoose = require('mongoose');


 var db = mongoose.connection;
 var adminSchema = new mongoose.Schema({
    username: String,
    name: String,
    hidden: {
        password: String,
        access: Boolean
    }
 });

 var branchSchema = new mongoose.Schema({
    admins: [adminSchema],
    location: String,
    hidden: {
        global_access: Boolean
    },
    token: String
 });

 var branch = mongoose.model('branch', branchSchema);
 var admin = mongoose.model('admin', adminSchema);

 exports.login = function(req,res){
    var user = new admin(req.body, null, true)
    user.hidden.password = sha1(user.hidden.password);
    console.log(user);

    mongoose.connect('mongodb://localhost:27017/branches');
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback()
    {
        console.log('connected to database');
        branch.findOne({
        },
        {
            "admins": 
            {
                $elemMatch : 
                {
                    username: user.username, 
                    'hidden.password': user.hidden.password,
                }
            }
        })
        .exec(
        function (err,doc){
            db.close();
            res.send(doc);
        });
    });
 };

我发了一封请求发送数据:

{
    username:'matthew',
    hidden.password:'200169+'
}

并返回结果:

{
    "_id": "532cec102e42bcfced71fe03",
    "admins": []
}
然而,我应该看到结果是这样的:

{
    "admins": [
        {
            "username":"matthew,
            "name":"lol"
        }
    ],
    "token":"f10e2821bbbea527ea02200352313bc059445190"
}

我怀疑查询是错误的,因为如果我发送用户名和密码的任意随机组合,它仍会给出相同的结果。请帮忙,谢谢!

1 个答案:

答案 0 :(得分:0)

代码中的findOne()语句存在问题。 findOne()的语法是:

db.collection.findOne(<criteria>, <projection>)

如果将空JSON传递给条件,则相当于不指定任何条件,MongoDB根据磁盘上文档的natural order返回第一个文档。由于您的查询包含空json作为查询条件并且仅提供投影条件,因此MongoDB始终返回磁盘上的第一个文档并对其应用投影条件。由于返回的文档与投影不匹配,因此您将获得一个空数组。

你应该尝试类似的东西:

    ...
    branch.findOne({
        "admins": 
        {
            $elemMatch : 
            {
                username: user.username, 
                'hidden.password': user.hidden.password,
            }
        }
    },
    {"admins.$":1, token:1}
    )
    ...