有没有更好的方法将 mongodb 查询导出到新集合?
我想要什么: 我有一个产品主集合,然后我想过滤它们并将它们放入一个单独的集合中。
db.masterproducts.find({category:"潜水装备"}).copyTo(db.newcollection)
当然,我意识到“copyTo”不存在。
我认为我可以使用 MapReduce 来做到这一点,因为结果是使用 v1.8 中新的“out”参数在新集合中创建的;然而,这个新收藏并不是我原来收藏的子集。或者如果我正确使用MapReduce 可以吗?
为了解决这个问题,我目前正在这样做: 步骤一: /usr/local/mongodb/bin/mongodump --db database --collection masterproducts -q '{category:"scuba gear"}'
步骤 2: /usr/local/mongodb/bin/mongorestore -d database -c newcollection --drop packages.bson
我的两步方法似乎效率相当低!
非常感谢任何帮助。
谢谢鲍勃
What I want:
I have a master collection of products, I then want to filter them and put them in a separate collection.
db.masterproducts.find({category:"scuba gear"}).copyTo(db.newcollection)
Of course, I realise the 'copyTo' does not exist.
I thought I could do it with MapReduce as results are created in a new collection using the new 'out' parameter in v1.8; however this new collection is not a subset of my original collection. Or can it be if I use MapReduce correctly?
To get around it I am currently doing this:
Step 1:
/usr/local/mongodb/bin/mongodump --db database --collection masterproducts -q '{category:"scuba gear"}'
Step 2:
/usr/local/mongodb/bin/mongorestore -d database -c newcollection --drop packages.bson
My 2 step method just seems rather inefficient!
Any help greatly appreciated.
Thanks
Bob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以迭代查询结果并保存每个项目,如下所示:
You can iterate through your query result and save each item like this:
您应该能够使用 mapreduce 创建子集(使用“out”)。问题是mapreduce有一种特殊的输出格式,因此你的文档将被转换(有一个JIRA票证可以添加对另一种格式的支持,但我目前找不到它)。这也将是非常低效的:/
将游标复制到集合中很有意义,我建议为此创建一个票证。
You should be able to create a subset with mapreduce (using 'out'). The problem is mapreduce has a special output format so your documents are going to be transformed (there is a JIRA ticket to add support for another format, but I can not find it at the moment). It is also going to be very inefficent :/
Copying a cursor to a collection makes a lot of sense, I suggest creating a ticket for this.
还有 toArray() 方法可以使用:
there is also toArray() method which can be used: