保存MongoDB查询的结果 [英] Saving the result of a MongoDB query

查看:53
本文介绍了保存MongoDB查询的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mongo shell中进行研究时,我经常编写非常复杂的查询,并希望将结果存储在其他集合中.我知道用.forEach()做到这一点的方法:

db.documents.find(query).forEach(function(d){db.results.insert(d)})

但是每次写这些东西都是很乏味的.有没有更清洁的方法?我希望语法类似于db.documents.find(query).dumpTo('collectionName').

解决方案

这是我将使用的解决方案:db.results.insert(db.docs.find(...).toArray())

尽管如此,仍然有太多的噪音.

UPD :还有一个使用聚合管道重写find的选项.然后,您可以使用 $out运算符.

When doing a research in mongo shell I often write quite complex queries and want the result to be stored in other collection. I know the way to do it with .forEach():

db.documents.find(query).forEach(function(d){db.results.insert(d)})

But it's kind of tedious to write that stuff each time. Is there a cleaner way? I'd like the syntax to be something like db.documents.find(query).dumpTo('collectionName').

解决方案

Here's a solution I'll use: db.results.insert(db.docs.find(...).toArray())

There is still too much noise, though.

UPD: There is also an option to rewrite find using aggregation pipeline. Then you can use $out operator.

这篇关于保存MongoDB查询的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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