update a specific object inside an array of object in mongodb [duplicate] - mongodb

This question already has answers here:
MongoDB: How do I update a single subelement in an array, referenced by the index within the array?
(10 answers)
How do I update Array Elements matching criteria in a MongoDB document?
(2 answers)
Closed 4 years ago.
I have the following document:
{
_id: objectId("5aa7abb3c7dc133b0007e6e"),
name: "first course",
targets: Array
0: Object
id: "11111_rrrr",
value: "test"
1: Object
id: "22222_rrrr",
value: "hi"
}
so, I want to change the value of the array[i].
my solution is :
$update = array(
'targets.$[].value' => "New Value"
);
$result = Course::where("_id", $course_id)->update($update);
it changes all the value of the array, how can I solve my problem??

Try this
db.getCollection('TEST').update({"targets.id" : $course_id},{$set:{"targets.$.value" : "New Value"}})

Related

Create a document local index over a array [duplicate]

This question already has answers here:
MongoDB: Unique index on array element's property
(6 answers)
Can you specify a key for $addToSet in Mongo?
(2 answers)
Closed 3 years ago.
I have the following structure in my Mongo document:
{
property1: 'value',
property2: 'value',
items: [
{
name: 'name1'
},
{
name: 'name2'
},
]
}
Is possible to define a local index over the items array?
In my case, item.name was a unique key but only in this document.
Other documents can have also a item with the same name that a item in other document (but the name in a document cannot be repeated).
Now I'm checking manually if the name is already in use before add/update elements in the document.items array, but if Mongo is capable to do it I will prefer...
EDIT: I have tried to create a index over items.name but if I create that index Mongo doesn't allows to insert/update a item if the name is already defined in other document.

Updating Every Record in Mongo [duplicate]

This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 6 years ago.
How would I update every record in the "title" collection for mongo, to make a field named LastReturnedName set to Name for every single record?
Thought it would be something like this:
db.title.update(
{ "LastNameReturned" : "" },
{ $set:{ "LastNameReturned" : Name } },
{ multi : true }
);
You can use foreach iteration:
db.title.find({ "LastNameReturned" : "" }).snapshot()
.forEach(function(t) {
db.title.update({_id: t._id}, {$set: {"LastNameReturned" : t.Name}});
});
NOTE: You can use snapshot() only with unsharded collections. Also you can speed up updating if you'll use bulk operation.

MongoDb: add element to array if not exists [duplicate]

This question already has answers here:
Can you specify a key for $addToSet in Mongo?
(2 answers)
Closed 6 years ago.
I'm using node.js and Mongo.db (I'm newly on Mongo). I have a document like this:
Tag : {
name: string,
videoIDs: array
}
The idea is, the server receives a JSON like
JSON: {
name: "sport",
videoId: "34f54e34c"
}
with this JSON, it has to find the tag with the same name and check if in the array it has the videoId, if not, insert it to the array.
How can I check the array and append data?
You can use $addToSet operator to check exist before append element into array.
db.tags.update(
{name: 'sport'},
{$addToSet: { videoIDs: "34f54e34c" } }
);
In this update statement example, mongoDB will find the TAG document which matches name == sport, and then check whether the videoIDs array contains 34f54e34c. If not, append it to the array.
Detail usage of $addToSet please read here.
I didn't test it, but you can try something like:
yourDb.find({ name: "sport",
videoIDs: { $in: ["34f54e34c"] } })
.update({$addToSet: { videoIDs: "34f54e34c" }});
If you need how: MongoDb in nodeJs implementation, you can start with:
http://jsfiddle.net/1ku0z1a1/
$addToSet operator check for the empty or existing array in document.
db.col_name.update({name :"name_for_match"},{$addToSet : { videoId : "video_id_value"}})

How to do update the matched array element [duplicate]

This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
Closed 6 years ago.
user1 in database
{
counts: [{code:"aaa", num:100}, {code:"bbb", num:200}]
}
counts is an Array, it has two elements code and num.
How to let num increase 1 if code = "aaa"?
Meteor.users.update(user1._id, {$inc: {???}}); // How to write this?
Thanks
Gotta positionally match. Example:
Players.update({
_id: Meteor.userId(),
'games._id': Session.get('gameId')
}, {
$inc: { 'games.$.score': 1 }
});

How to get attributes from object which is in another one? [duplicate]

This question already has answers here:
MongoDB: query for a field
(1 answer)
MongoDB - how to query for a nested item inside a collection?
(3 answers)
Closed 8 years ago.
here is an example structure of one object in my MongoDB:
{
"itemClass": 4,
"containerSlots": 0,
"itemBind": 1,
"weaponInfo": {
"dps": "0.0",
"damage": {
"maxDamage": 0,
"minDamage": 0
},
"weaponSpeed": "0.5"
}
}
I need to find maxDamage which is 1
here is my query:
db.items.find({"maxDamage" : "1"}).limit(3)
but it returns nothing
p.s. there are a lot of objects with attribute maxDamage is 1
You have to use dot notation like this:
db.items.find({"weaponInfo.damage.maxDamage": 1})