English 中文(简体)
• 如何将多美元组合合并为合计mongodb的美元项目?
原标题:How to merge multiple $group into $project in aggregate mongodb?

我正在使用总合数据对MongoDB的询问数据,但我有一组问题,我想把多美元小组阶段合并为一美元项目,最后看上去似乎与我所期望的一样。 在下面的法典中,我只字不提:<条码>。 我如何解决这一问题?

这里是我的总管道:

const user = userCollection.aggregate([
    {
      $match: {
         _id: ObjectId(id)
      }
    },

    // lookup images list
    {
       $lookup: {...}
    },
    {
        $unwind: {
            path:  $images ,
            preserveNullAndEmptyArrays: true,
        },
    },
    { $sort: {  images.createdAt : -1 } },
    {
        $group: {
            _id:  $_id ,            
            images: {
                $push:  $images ,
            },
        },
    },

    // lookup blocked list
    {
       $lookup: {...}
    },
    {
        $unwind: {
            path:  $blocked ,
            preserveNullAndEmptyArrays: true,
        },
    },
    { $sort: {  blocked.createdAt : -1 } },
    {
        $group: {
            _id:  $_id ,            
            blocked: {
                $push:  $blocked ,
            },
        },
    },

    // lookup followers list
    {
       $lookup: {...}
    },
    {
        $unwind: {
            path:  $followers ,
            preserveNullAndEmptyArrays: true,
        },
    },
    { $sort: {  followers.createdAt : -1 } },
    {
        $group: {
            _id:  $_id ,            
            followers: {
                $push:  $followers ,
            },
        },
    },


    {
       $project: {
          _id: 1,
          name: 1,
          age: 1,
          bio: 1,
          images: 1,
          blocked: 1,
          followers: 1,
       }
    }

]);

console.log(user);
// Results: [{ _id:  ...  }, { _id:  ...  }, { _id:  ...  }, { _id:  ...  }]
问题回答

注意的一般准则是,处理阵列的顺序如下:

  1. $unwind
  2. Processing the unwound array, eg filtering it with $match or $sorting it
  3. $grouping the data back together using `_id: $_id

任何时候,你都会看到,你应努力寻找能够处理阵列的其他操作者,而不必拆除文件,然后重建文件。

在你的情况中,我期待着采取类似行动:

[
  { $match: { ... } },
  { $lookup: { ... as:  images  } },
  { $lookup: { ... as:  blocked  } },
  { $lookup: { ... as:  followers  } },
  {
     $project: {
      _id: 1,
      name: 1,
      age: 1,
      bio: 1,
      images: {
        $sortArray: { input: "$images", sortBy: { createdAt: -1 } }
      },
      blocked: {
        $sortArray: { input: "$blocked", sortBy: { createdAt: -1 } }
      },
      followers: {
        $sortArray: { input: "$followers", sortBy: { createdAt: -1 } }
      },
     }
  }
]

具体来说,我们正在使用。 在最后<代码>项目阶段使用的代码>,以进行所提及的阵列处理。

可在上找到。

或者,如果在提供该经营者之前的版本上出现,或者如果需要额外的逻辑,那么你可以考虑使用

[
  ...
  { $lookup: 
    {
      from:  images ,
      localField:  imageField ,
      foreignField:  imageField ,
      pipeline: [
        { $sort: { createdAt: -1 } }
      ]
      as:  images 
    } 
  },
  ...
]




相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...

热门标签