【问题标题】:Mongoose/MongoDB Aggregation - $match $unwind $match not working?Mongoose/MongoDB 聚合 - $match $unwind $match 不起作用?
【发布时间】:2014-08-05 07:16:47
【问题描述】:

我将如何更改此存档视图聚合以也聚合 from.view.archive :true?

..同时防止重复消息的聚合..如果 To 数组中存在 From User (sessionUser),则只获取此消息之一..

 if (archive === true) {
  console.log('archive === ' + true);

  Models.Message.aggregate([

     // Match documents
     { "$match": {
          "to": { 
              "$elemMatch": {
                 "username": req.session.username,
                 "view.archive": true,
                 "view.bin": false
              }
          },
          "$or": messagingquery
     }},

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

     // Match the array elements      
     { "$match": {
         "to.username": req.session.username,
         "to.view.archive": true,
         "to.view.bin": false
     }},

     // Group back to the original document
     { "$group": {
         "_id": "$_id",
         "from": { "$first": "$from" },
         "to": { "$push": "$to" },
         "message": { "$first": "$message" },
         "timesent": { "$first": "$timesent" },
         "replies": { "$first": "$replies" },
         "messaging": { "$first": "$messaging" }
     }},

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

  ], function (err, messages) {

    if (err) {
      console.log(err);
      res.send(err);
    }

    res.json({
      messages : messages,
      sessionUser: sessionUser
    });

  });

}

对于 MessageSchema 的 From 和 To 数组,UserMessageSchema 看起来像这样:

var UserMessageSchema   = new Schema({
  user        : { "type": Schema.ObjectId, "ref": "User" },
  username    : String,
  view : {
    inbox       : Boolean,
    outbox      : Boolean,
    archive     : Boolean,
    bin         : Boolean
  },
  read : {
    marked      : { "type": Boolean, default: false },
    datetime    : Date
  },
    updated   : Date
});

【问题讨论】:

    标签: node.js mongodb if-statement mongoose aggregation-framework


    【解决方案1】:

    我觉得我对这种情况一定有误解,但听起来你应该可以在第一场比赛阶段添加一个简单的$or

    { "$match": {
        "$or" : [
            {
                "to": { 
                      "$elemMatch": {
                          "username": req.session.username,
                          "view.archive": true,
                          "view.bin": false
                      }
                }
            },
            {
                "from" : {
                    "$elemMatch" : {
                        "username" : req.session.username,
                        "view.archive": true,
                    }
                }
            }
        ],
            "$or": messagingquery
     }}
    

    $match 阶段不会导致重复,因为$match 根据条件过滤文档。重复来自哪里?“重复”究竟是如何定义的?

    【讨论】:

    • 非常感谢您的回答。 $or 是否获取与 to.view.archive:true 匹配的所有文档以及与 from.view.archive:true 匹配的所有文档?这会导致 res.json({messages}); 中出现多个相同的文档吗?如果文档同时符合这两个条件?
    • 每个文档只能出现一次结果结果列表。如果您有一个带有 {a:[1,2,3]} 的文档,并且您查询 a 为 1 或 a 为 2 或 a 为 3 的所有文档,即使它匹配所有三个或',您也只会返回一个文档ed 条件。
    • $unwind 阶段或第二个 $match 阶段或 $group 阶段是否需要修改?仍然没有从...中检索到任何东西
    • 第二个 $match 阶段应该与第一个阶段相同 - 您必须同时更改。
    • @AsyaKamsky 谢谢.. 这应该包括 $or 吗?就像字面上一样? $unwind 呢?
    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2017-08-20
    • 1970-01-01
    • 2020-08-07
    • 2016-07-11
    相关资源
    最近更新 更多