mongodb - mongo聚合查询使用$out输出,执行的是替换操作而不是append操作
PHPz
PHPz 2017-05-02 09:18:44
0
1
835

mongo聚合查询遇到的一个问题

当前对mongo数据库的一张表做一些聚合查询的操作,考虑到数据量太大,从数据库查寻出来结果还是需要插入到数据库,故决定,将查询结果插入到一个临时的集合中,使用mongo的$out操作。根据业务需求,需要执行三次这样的聚合查询,测试发现$out输出到新的集合是,执行替换操作,而不是append操作,以下是官网解释:

  • The $out operation creates a new collection in the current database if one does not already exist.

  • If the collection specified by the $out operation already exists, then upon completion of the aggregation, the $out stage atomically replaces the existing collection with the new results collection. The $out operation does not change any indexes that existed on the previous collection.

现在我依然有这样的需求,将多次聚合的结果放在一张表中,该怎么做?

PHPz
PHPz

学习是最好的投资!

reply all(1)
滿天的星座

Starting from 2.6, the return value of aggregation is a cursor. Since it is a cursor, you are not required to cache the entire result set in memory on the client before operating it. The straightforward idea is to traverse the cursor and insert it into the target collection one by one. For example, in the shell:

db.coll.aggregate([...]).forEach(function(doc) {
  db.target_coll.save(doc);
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template