保存MongoDB查询的结果 [英] Saving the result of a MongoDB query
问题描述
在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屋!