MongoDB在查找查询

时间:2016-05-03 08:12:43

标签: mongodb mongodb-query nosql

我通过Ubuntu虚拟机使用MongoDB和Mongochef GUI。我需要执行一个查询,插入我之前找到的数据。

我该怎么做?我想到这样的事情:

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

GMAP是我拥有的其他集合,查找查询返回所需的数据。 感谢

2 个答案:

答案 0 :(得分:4)

要解决这个问题,我们需要将结果存储为数组然后插入 - 请在下面找到代码段:

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

答案 1 :(得分:1)

要排除某些字段,请使用find 投影选项:在此示例中,“_id”字段已排除,可用于插入同一集合。

要插入许多文档,请使用 insertMany 方法:

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