[英]mongodb insert a new field into an array
I am trying to insert a new field to an array of objects我正在尝试向对象数组插入一个新字段
{
"_id": "543356fe0c9af6066e68970c",
"createdDate": "2014-10-06 07:59 pm",
"cancle": false,
"eventDate": "2014/12/12",
"eventstatus": true,
"location": "chennai",
"userId": "54310801e2659ecc3650100b",
"createdBy": "one",
"eventName": "tea ",
"__v": 0,
"friends": [
{
"userId": "54310814e2659ecc3650100c",
"userName": "two",
"phoneNumber": "22222222"
},
{
"userId": "54310945e2659ecc3650100d",
"userName": "three",
"phoneNumber": "33333333"
},
{
"userId": "54334def7e85de48638d1069",
"userName": "four",
"phoneNumber": "44444444"
}
]
}
I want to add "status": 0 to all the objects inside friends array我想将 "status": 0 添加到 friends 数组中的所有对象
I tried using addtoset, push and multiple ways, its adding as a new object in friends array.我尝试使用 addtoset、push 和多种方式,将其添加为好友数组中的新 object。
db.events.update({},{$push:{ "friends" : {"status" : 0} }},false,true)
db.events.update({},{$addToSet : {'friends' : $each: [{ 'status':0}]}},false,true)
Please let me know what i am doing wrong.请让我知道我做错了什么。 Thanks for your help谢谢你的帮助
You can use this to update the arrays 您可以使用它来更新阵列
db.events.find().forEach(function(e){
var t=e.friends;
t.forEach(function(e){
e.status=0
});
db.events.update({_id:e._id},{$set:{friends:t}})
})
Run this in your mongo terminal. 在你的mongo终端中运行它。
Don't know about problem with the code but I have the right code for it 不知道代码的问题,但我有正确的代码
var cursor = db.events.find();
cursor.forEach(function(item)
{
var friends = item.friends;
friends.forEach(function(elem)
{
elem['status'] = '....',
})
db.events.update({_id: item._id}, item);
});
It is working with mongo shell, but I don't know about javascript 它正在使用mongo shell,但我不知道javascript
In Java and Springs MongoTemplate you can add a new field to all entries in a array with: 在Java和Springs MongoTemplate中,您可以向数组中的所有条目添加一个新字段:
final Query query = new Query(Criteria.where("states.$[].date").exists(false));
final Update update = new Update().set("states.$[].stateSetBy", "SYSTEM");
mongoTemplate.updateMulti(query, update, "<collection_name>");
in this example you have a document with an array of "states" and want to add a new field to every entry of this array. 在此示例中,您有一个包含“状态”数组的文档,并希望为此数组的每个条目添加一个新字段。
most important part is the $[] to address every entry in the array 最重要的部分是$ []来解决数组中的每个条目
see: https://docs.mongodb.com/manual/reference/operator/update-array/ 请参阅: https : //docs.mongodb.com/manual/reference/operator/update-array/
This answer can also help you: https://stackoverflow.com/a/67162997/7724011这个答案也可以帮助你: https://stackoverflow.com/a/67162997/7724011
It uses the updateMany
method with aggregation pipeline to iterate over the array items and then use $mergeObjects
to add new properties to each one of them.它使用带有聚合管道的updateMany
方法迭代数组项,然后使用$mergeObjects
为每个项添加新属性。
up时需要添加多个true和call函数
db.events.update({},{$push:{ "friends" : {"status" : 0} }},{ multi: true },function(e,update){});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.