Mongo:导入聚合查询的结果?

时间:2014-12-17 15:26:27

标签: mongodb

对不起基本的问题,我是mongo的新手并且学习我的方式。

我在Mongo中运行了一个聚合查询:

> var result = db.urls.aggregate({$group : {_id : "$pagePath"} });
> result
{ "_id" : "/section1/page1" }
{ "_id" : "/section1/page2" }
...
Type "it" for more

我现在想将此聚合查询的结果保存到新集合中。这就是我尝试过的:

> db.agg1.insert(result);
WriteResult({ "nInserted" : 1 })

但是这似乎只将所有行插入一行:

> db.agg1.count()
1
> db.agg1.findOne();
{ "_id" : "/section1/page1" }
{ "_id" : "/section1/page2" }
...
Type "it" for more

如何将这些插入单独的行?

我尝试直接插入_id,但没有成功:

> db.agg1.insert(result._id);
2014-12-17T15:23:26.679+0000 no object passed to insert! at src/mongo/shell/collection.js:196

1 个答案:

答案 0 :(得分:1)

使用$out管道运算符:

db.urls.aggregate([
    {$group : {_id : "$pagePath"} },
    {$out: "agg1"}
]);

请注意,MongoDB 2.6中添加了$out