如何将集合中的多个文档作为数组字段插入另一个文档-MongoDB
我正在尝试将集合“documents”中的所有文档作为数组插入到新文档中。新文档应该是这样的:如何将集合中的多个文档作为数组字段插入另一个文档-MongoDB,mongodb,Mongodb,我正在尝试将集合“documents”中的所有文档作为数组插入到新文档中。新文档应该是这样的: { id=1, arr=[all the documents from the collection] } 它试图: db.results.insert({"id":2,"arr":[]}); db.documents.find().forEach(function(doc){ db.results.update( { "id": 2 }, {
{
id=1,
arr=[all the documents from the collection]
}
它试图:
db.results.insert({"id":2,"arr":[]});
db.documents.find().forEach(function(doc){
db.results.update(
{ "id": 2 },
{ $push: { "arr": doc } })
})
它不起作用。
谢谢我想起来了:
我需要添加标志:
db.results.update({"id": 2}, {$push: {"arr": doc }}, true, false)
你的代码非常适合我。不知道为什么它对你不起作用。你能再加一点细节吗?我总是在结尾处有一个空白数组