有没有更好的方法将 mongodb 查询导出到新集合?

发布于 2024-11-16 06:09:12 字数 589 浏览 6 评论 0原文

我想要什么: 我有一个产品主集合,然后我想过滤它们并将它们放入一个单独的集合中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心荒芜 2024-11-23 06:09:12

您可以迭代查询结果并保存每个项目,如下所示:

db.oldCollection.find(query).forEach(function(x){db.newCollection.save(x);})

You can iterate through your query result and save each item like this:

db.oldCollection.find(query).forEach(function(x){db.newCollection.save(x);})
北城半夏 2024-11-23 06:09:12
  1. 您可以创建小型服务器端 JavaScript(例如 这个,只需添加您想要的过滤)并使用 eval
  2. 您可以按照上述方式使用转储/恢复复制
  3. 收集命令应该很快就会出现在mongodb中(将按投票顺序完成)!请参阅 jira 功能。
  1. You can create small server side javascript (like this one, just add filtering you want) and execute it using eval
  2. You can use dump/restore in the way you described above
  3. Copy collection command shoud be in mongodb soon (will be done in votes order)! See jira feature.
A君 2024-11-23 06:09:12

您应该能够使用 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.

梦里梦着梦中梦 2024-11-23 06:09:12

还有 toArray() 方法可以使用:

//create new collection 
db.creatCollection("resultCollection")

// now query for type="foo" and insert the results into new collection
db.resultCollection.insert( (db.orginialCollection.find({type:'foo'}).toArray())

there is also toArray() method which can be used:

//create new collection 
db.creatCollection("resultCollection")

// now query for type="foo" and insert the results into new collection
db.resultCollection.insert( (db.orginialCollection.find({type:'foo'}).toArray())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文