简体   繁体   English

MongoDB查找查询后插入数据

[英]MongoDB insert data after find query

I'm using MongoDB with Mongochef GUI over an Ubuntu Virtual Machine. 我在Ubuntu虚拟机上使用MongoDB和Mongochef GUI。 I need to do a Query which insert the data that I have found previously. 我需要执行一个查询,该查询将插入我之前找到的数据。

How can i do this? 我怎样才能做到这一点? I thought something like this: 我认为是这样的:

db.createCollection("prueba", { capped : true, size : 5242880, max : 5000 } )
db.gmap.find( { emotion: 1 } )
db.prueba.insertMany(db.gmap.find( { emotion: 1 } ))

GMAP is other collection that i have and the find query returns needed data. GMAP是我拥有的其他集合,并且find查询返回所需的数据。 Thanks 谢谢

To solve that we need to store result as an array and then insert - please find snippet below: 为了解决我们需要将结果存储为数组然后插入的问题,请在下面找到以下代码段:

var a = db.sourceCollection.find().toArray()
db.destinatioCollection.insert(a)

To exclude some fields use the find projection option: in this example the "_id" field is excluded, usefull to insert in the same collection. 要排除某些字段,请使用“查找投影”选项:在此示例中,排除了“ _id”字段,可用于插入同一集合中。

To insert many documents use insertMany method: 要插入许多文档,请使用insertMany方法:

db.dstColl.insertMany( db.srcColl.find({}, {"_id": false}).toArray() )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM