【问题标题】:Mongoose find field in array not working猫鼬在数组中查找字段不起作用
【发布时间】:2014-03-25 06:41:28
【问题描述】:

我有一个在 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"
}

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

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您的代码中的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}
        )
        ...
    

    【讨论】:

    • 感谢您的回答!但是我仍然收到其他管理员的条目(matthew1),这可能与投影语句有关吗?
    • 我已编辑帖子以包含投影。您现在应该会看到预期的结果。
    猜你喜欢
    • 2016-10-13
    • 2016-11-04
    • 2019-12-02
    • 2020-07-25
    • 2020-09-12
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多