Mongodb .aggregate不匹配$ or? [英] Mongodb .aggregate not matching $or?

查看:198
本文介绍了Mongodb .aggregate不匹配$ or?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论用户发送还是接收消息,他/她都应该能够为他/她自己归档消息.

Whether a user sends or receives a message, he/she should be able to archive the message for him/herself.

此.aggregate $ or,但是,仅返回文档,该文档是用户是他/她标记为"archive":"true"的("to")的收件人.它会根据值"布尔值准确地过滤邮件.

This .aggregate $or, however, only returns docs that the user is the recipient of ('to') that he/she has marked 'archive':'true'. It does accurately filter messages according to the 'value' Booleans.

Models.Messages.aggregate([

    // Match documents
    { "$match": {
      "$or" : [
          {
              "to": { 
                    "$elemMatch": {
                        "username": 'userA',
                        "view.archive": true,
                        "view.bin": false
                    }
              }
          },
          {
              "from" : {
                  "$elemMatch" : {
                      "username" : 'userA',
                      "view.archive": true,
                      "view.bin": false
                  }
              }
          }
      ],
      "$or": [
               { 'value.1': true },
               { 'value.2': true },
               { 'value.3': true },
               { 'value.4': false } 
             ]
    }},

    // Unwind to de-normalize
    { "$unwind": "$to" },
    { "$unwind": "$from" },

    // Match the array elements      
    { "$match": {
       "to.username": 'userA',
       "to.view.archive": true,
       "from.username": 'userA',
       "from.view.archive": true
    }},

    // Group back all the elements of the original document
    { "$group": {
       "_id": "$_id",
       "from": { "$push": "$from" },
       "to": { "$push": "$to" },
       "message": { "$first": "$message" },
       "updated": { "$first": "$updated" }
    }},

    // Sort by updated, most recent first (descending)
    {"$sort": {"updated": -1}}

], function (err, docs) {

如何更改它,以便将以下两个消息的所有字段汇总到userA:

How could this be altered so that all the fields of both of the following messages are aggregated for userA:

{
    "_id" : ObjectId("53e83867f316ea7f22fd3b2b"),
    "updated" : ISODate("2014-08-11T03:29:06.000Z"),
    "message" : "message1",
    "value" : [
                "1" : true,
                "2" : false,
                "3" : false,
                "4" : false
            ]
    "to" : [ 
        {
            "user" : ObjectId("53e835bd76e0d04318d8cc4e"),
            "username" : "userA",
            "_id" : ObjectId("53e83867f316ea7f22fd3b2c"),
            "view" : {
                "inbox" : false,
                "outbox" : false,
                "archive" : true,
                "bin" : false
            }
        }
    ],
    "from" : [ 
        {
            "user" : ObjectId("53e8360276e0d04318d8cc55"),
            "username" : "userB",
            "_id" : ObjectId("53e83867f316ea7f22fd3b2d"),
            "view" : {
                "inbox" : false,
                "outbox" : true,
                "archive" : false,
                "bin" : false
            }
        }
    ],
    "__v" : 5
}
{
    "_id" : ObjectId("53e83867f316ea7f22fd3b2b"),
    "updated" : ISODate("2014-08-11T03:29:06.000Z"),
    "message" : "message2",
    "value" : [
                "1" : false,
                "2" : true,
                "3" : false,
                "4" : false
            ]
    "to" : [ 
        {
            "user" : ObjectId("53e8360276e0d04318d8cc55"),
            "username" : "userB",
            "_id" : ObjectId("53e83867f316ea7f22fd3b2c"),
            "view" : {
                "inbox" : true,
                "outbox" : false,
                "archive" : false,
                "bin" : false
            }
        }
    ],
    "from" : [ 
        {
            "user" : ObjectId("53e835bd76e0d04318d8cc4e"),
            "username" : "userA",
            "_id" : ObjectId("53e83867f316ea7f22fd3b2d"),
            "view" : {
                "inbox" : false,
                "outbox" : true,
                "archive" : true,
                "bin" : false
            }
        }
    ],
    "__v" : 5
}

推荐答案

第一个$match对象的顶层不能有两个$or字段,因此需要将它们包装在$and中如果都需要满足以下条件,则如下所示:

You can't have two $or fields at the top level of your first $match object, so you need to wrap those in an $and array like the following if they both need to be satisfied:

{ "$match": { 
  $and: [{
    "$or" : [
        {
            "to": { 
                  "$elemMatch": {
                      "username": 'userA',
                      "view.archive": true,
                      "view.bin": false
                  }
            }
        },
        {
            "from" : {
                "$elemMatch" : {
                    "username" : 'userA',
                    "view.archive": true,
                    "view.bin": false
                }
            }
        }
    ]}, {
    "$or": [
             { 'value.1': true },
             { 'value.2': true },
             { 'value.3': true },
             { 'value.4': false } 
           ]
    }
  ]
}}

您的管道中正在进行很多事情,因此这可能不是唯一的问题.

There's a lot going on in your pipeline, so this may not be the only problem.

这篇关于Mongodb .aggregate不匹配$ or?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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