简体   繁体   English

如何在MongoDb中更新数组值

[英]How to update array value in MongoDb

$set is overtiring complete array value how to update it $ set淘汰了完整的数组值,如何更新它

                var obj = // contains some other data to update as well 
                obj.images=images;  //  updating obj with images []
                Units.update({_id: 'id', {$set: obj});

finally my mongo object must be something like this 最后我的mongo对象一定是这样的

{
"_id" : "ZhBgrNFtr2LRPkQrZ",
"sitouts" : "test",
"interiors" : "test",
"features" : "test",
"keyHolder" : "test",
"images" : [
    "1445481257465-8678-20130520_113154.jpg", ## `updated value ` ##
    "1445481257456-3-20130520_113157.jpg",
    "1445481258058-5771-20130520_113202.jpg",
    "1445481258230-9603-20130521_075648.jpg"
]

} }

https://docs.mongodb.org/manual/reference/operator/update/push/ $push, $each, $slice. https://docs.mongodb.org/manual/reference/operator/update/push/ $ push,$ each,$ slice。 See the documentation. 请参阅文档。

Here are a few ways to update the images array without overwriting it: 以下是几种在不覆盖图像的情况下更新图像数组的方法:

Update a Specific Index 更新特定索引

Units.update({_id: id}, {$set: {'images.0': obj.images[0]}})

Append a Single Value 附加一个值

Units.update({_id: id}, {$push: {images: obj.images[0]}})

Append Multiple Values 附加多个值

Units.update({_id: id}, {$push: {images: {$each: obj.images}}})

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

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