Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在mongodb中的$or语句中组合两个$and语句?_Mongodb - Fatal编程技术网

如何在mongodb中的$or语句中组合两个$and语句?

如何在mongodb中的$or语句中组合两个$and语句?,mongodb,Mongodb,我正在mongodb中搜索从a到B的所有消息以及从B到a的所有声明。这样我就可以进行对话 发件人:人员A和收件人:人员B 或 从:人物B到人物角色 // Create a conversation db.collection('messages', function (err, collection) { collection.find( { // how do I turn this $and into a two nested $and statements insi

我正在mongodb中搜索从a到B的所有消息以及从B到a的所有声明。这样我就可以进行对话

发件人:人员A收件人:人员B

从:人物B到人物角色

// Create a conversation
db.collection('messages', function (err, collection) {
    collection.find(
        { // how do I turn this $and into a two nested $and statements inside $or?
            $and: [{
                receiver: new BSON.ObjectID(req.user._id)
            }, {
                sender: new BSON.ObjectID(req.body.sender)
            }]
        }
    ).sort({
        date: -1
    }).toArray(function (err, docs) {
        console.log(docs);
    })
});

答案应该是这样的:

db.collection('messages', function (err, collection) {
    collection.find(
        { 
        $or : [         
            {$and: [{
                receiver: new BSON.ObjectID(req.user._id)
            }, {
                sender: new BSON.ObjectID(req.body.sender)
            }]},
            {$and: [{
                receiver: new BSON.ObjectID(req.body.sender)
            }, {
                sender: new BSON.ObjectID(req.user._id)
            }]},
        ]
        }
    ).sort({
        date: -1
    }).toArray(function (err, docs) {
        console.log(docs);
    })
});
试试这个

db.collection('messages', function (err, collection) {
    collection.find(
        {  $or: [
                    {$and: [{ receiver: new BSON.ObjectID(req.user._id)}, {sender: new BSON.ObjectID(req.body.sender)}]}
                    {$and: [{ receiver: new BSON.ObjectID(req.body.sender)}, {sender: new BSON.ObjectID(req.user._id)}]}
        ]
}).sort({
        date: -1
    }).toArray(function (err, docs) {
        console.log(docs);
    }) });

我不熟悉您正在调用的
collection
函数,也不知道您所指的
req
对象是什么,所以请对我的回答持保留态度

在我看来,你把事情弄得比实际需要的复杂得多。您的
$和
语句非常简单,不需要
$和
关键字:

collection.find({
    receiver: req.user._id,
    sender:   req.body.sender
})
现在,
$和
$或
的工作方式完全相同:它们获取一个对象数组。那么,让我们写下我假设您希望查询的内容:

collection.find({
    $or: [{
        receiver: req.user._id,
        sender:   req.body.sender
    }, {
        receiver: req.body.sender,
        sender:   req.user_id
    }]
})

就像$and一样,只需将整个$and查询用作$orThanks DISPOWER的一个参数!我是否将整个$and括在花括号中?我已经省略了查询(排序,…)的关键部分,以集中于我对您实际问题的理解。复制/粘贴此代码来代替以前的代码显然是行不通的。