【问题标题】:mongo Queries With Multiple Expressions Specifying the Same Operator具有多个指定相同运算符的表达式的 mongodb 查询
【发布时间】:2019-03-20 12:10:06
【问题描述】:

您好,我正在尝试在我的数据库中执行以下查询

function getChat(nameChat, nameUser, callback) {
global.db.collection('chatUsers').find({ 
    $and: [ 
        { $or : [ { nameChat: nameChat }, { nameChat: nameUser } ] },
        { $or : [ { nameUser: nameChat }, { nameUser: nameUser } ] }
    ]
}).sort({date: 1}).toArray(
    function (err, docs) {
        if (err) return console.log("algo deu errado" + err);
        console.log("exit\n"+docs+nameUser);
        callback(docs);
    }
);}

但查询返回空 这是我在聊天用户集合中拥有的对象的示例:

{"_id":"5bc3e6d060d4da04dcd66517","nameChat":"Daciolo bolsominion","nameUser":"Mc Xhamps","date":"2018-10-14T17:22:00.000Z","msg":"Você pertence a URSAL"}

{"_id":"5bc52fc3b878631f84c77c41","nameChat":"Mc Xhamps","nameUser":"Daciolo bolsominion","date":"2018-10-01T17:25:00.000Z","msg":"E ai, bora ser Presidente"}

为了明确,我希望我的查询返回名称为 Chat 或名称 User 的所有对象都存在于同一对象中 此查询仅返回空对象。

【问题讨论】:

    标签: node.js mongodb mongodb-query nodes node-modules


    【解决方案1】:

    Hey Erik 改变 and 或者你会得到所需的结果。

    global.db.collection('chatUsers').find({ 
        $or: [ 
            { $or : [ { nameChat: nameChat }, { nameChat: nameUser } ] },
            { $or : [ { nameUser: nameChat }, { nameUser: nameUser } ] }
        ]
    }).sort({date: 1})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多