简体   繁体   English

MongoDB-尝试将整个集合的内容传输到另一个集合中的一个“用户对象”

[英]MongoDB - trying to transfer contents of an entire collection to one 'user object' in another collection

I've been collecting records for myself for some time now, so I have one collection entirely of my own records. 我已经为自己收集记录已有一段时间了,所以我有一个完全属于自己的记录。 Now I'm trying to add multi user capability and want to transfer all my data from that collection to an array within my own user object in another collection. 现在,我试图添加多用户功能,并希望将所有数据从该集合传输到另一个集合中我自己的用户对象内的数组。

This is where I'm at. 这就是我的位置。

myRecordsCollection.find({}).forEach(function(err, doc) {
    usersCollection.update(
        {
            "name": "Me"
        },
        {$push:
            {
                'records' : doc
            }
        }
    );
});

But this doesn't work. 但这是行不通的。 :( :(

$push push only one item at a time,But you are trying to push array,You need to use $each with $push. $ push一次只推送一项,但是您要推送数组,则需要将$ each与$ push一起使用。

myRecordsCollection.find({}).forEach(function(err, doc) {
    usersCollection.update(
        {
            "name": "Me"
        },
        {$push:
            {
                'records' : { $each: doc }
            }
        }
    );
});

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

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