mongodb 如何通过一次调用将一组对象推送到 mongoose 中的数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11963684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:47:45  来源:igfitidea点击:

How to push an array of objects into an array in mongoose with one call?

mongodbmongoose

提问by Pranil Dasika

I need to push multiple values into an array in mongoose using one call. I tried doing it using a smaller array but the array is getting inserted as a sub-array.

我需要使用一次调用将多个值推送到 mongoose 中的一个数组中。我尝试使用较小的数组来执行此操作,但该数组已作为子数组插入。

var kittySchema = new mongoose.Schema({
        name: String,
        values: [Number]
});

var Kitten = db.model('Kitten', kittySchema);
Kitten.update({name: 'fluffy'},{$push: {values:[2,3]}},{upsert:true},function(err){
        if(err){
                console.log(err);
        }else{
                console.log("Successfully added");
        }
});

The result of the calling the above code thrice gives the below result:

三次调用上述代码的结果如下:

{ "_id" : ObjectId("502b0e807809d79e84403606"), "name" : "fluffy", "values" : [ [ 2, 3 ], [ 2, 3 ], [ 2, 3 ] ] }

Whereas what I want is something like this:

而我想要的是这样的:

{ "_id" : ObjectId("502b0e807809d79e84403606"), "name" : "fluffy", "values" : [ 2, 3 ,2 ,3, 2, 3] }

Another thing I noticed was that the type in the array (values) is specified as Number, then wouldnt the 'strict' option ensure that anything other than Numbers are not inserted ? In this case another array is being allowed to be inserted.

我注意到的另一件事是数组中的类型(值)被指定为数字,那么“严格”选项不会确保不插入数字以外的任何内容吗?在这种情况下,允许插入另一个数组。

采纳答案by Amit Portnoy

(Dec-2014 update)Since MongoDB2.4 you should use:

(2014 年 12 月更新)从 MongoDB2.4 开始,您应该使用:

Kitten.update({name: 'fluffy'}, {$push: {values: {$each: [2,3]}}}, {upsert:true}, function(err){
        if(err){
                console.log(err);
        }else{
                console.log("Successfully added");
        }
});

回答by Stennie

Deprecatedsee other solution below using $push $each

不推荐使用 $push $each 查看下面的其他解决方案

Your example is close, but you want $pushAllrather than $pushto have each value added separately (rather than pushing another array onto the valuesarray):

您的示例很接近,但您希望$pushAll而不是$push分别添加每个值(而不是将另一个数组推送到values数组上):

var Kitten = db.model('Kitten', kittySchema);
Kitten.update({name: 'fluffy'},{$pushAll: {values:[2,3]}},{upsert:true},function(err){
        if(err){
                console.log(err);
        }else{
                console.log("Successfully added");
        }
});

回答by Dominic

Or use the $each modifier with $addToSet:

或者在 $addToSet 中使用 $each 修饰符:

https://docs.mongodb.com/manual/reference/operator/update/addToSet/#each-modifier

https://docs.mongodb.com/manual/reference/operator/update/addToSet/#each-modifier

// Existing tags array
{ _id: 2, item: "cable", tags: [ "electronics", "supplies" ] }

// Add "camera" and "accessories" to it
db.inventory.update(
   { _id: 2 },
   { $addToSet: { tags: { $each: [ "camera", "accessories" ] } } }
 )
// Existing tags array
{ _id: 2, item: "cable", tags: [ "electronics", "supplies" ] }

// Add "camera" and "accessories" to it
db.inventory.update(
   { _id: 2 },
   { $addToSet: { tags: { $each: [ "camera", "accessories" ] } } }
 )