How to do update the matched array element [duplicate] - mongodb

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 }
});

Related

Concatenate array with existing array in document [duplicate]

This question already has answers here:
How to push an array of objects into an array in mongoose with one call?
(4 answers)
Closed 3 years ago.
I have an array of stages in my MentorProfile document and I want to concatenate lessonsWithComments array with stages. Is this even possible without having to loop through the lessonsWithComments and then querying for each iteration with updateMany
MentorProfile.updateMany(
{},
{
$push: {
stages: {
lessonsWithComments
}
}
}
).then((update) => res.json(update));
I hope this helps you.
MentorProfile.updateMany(
{},
{ $push: { stages: { $each: lessonsWithComments } } }
).then((update) => res.json(update));
See: https://docs.mongodb.com/manual/reference/operator/update/push/

How to update array with Meteor [duplicate]

This question already has answers here:
Push items into mongo array via mongoose
(11 answers)
Closed 4 years ago.
I'm trying to add an element in an array but can't figure out what is causing my problem,
Don't know if i have to do an insert or an update anyway tried both, both not working
addUserToArray:function(userId,_id,email){
check(_id, String)
check(userId,String)
check(email, String)
var current_test = Modules.test.checkTestExist(test_id);
const USER = Accounts.findUserByEmail(email)
let test=USER._id
if(userId==current_test.senders[0]){
Tests.update(_id, {$set: {'name': name}}) // this one getting updated
Tests.update(test_id, { $set: { "current_test.senders": test} }) // this is not working
}
i got the error
UserIds that can send messages on this test must be an array",
You just need to change the $set by $push
Tests.update(box_id, { "$push": { "current_test.senders": USER._id} })

Search Specific Element In array mongodb [duplicate]

This question already has answers here:
How to filter array in subdocument with MongoDB [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to find a specific object from array of objects in mongodb.
I am trying this
Company.findOne ({
"configuration.macAddress": "AB-90-dF-8d"
});
It returns me the exact company but it returns all the configuration array
I want only configuration with matching macAddress
Instead use aggregate(). $unwind the configuration array first, then you can $match the specific element only.
Company.aggregate([
{
"$unwind": "$configuration"
},
{
"$match":{
"configuration.macAddress": "AB-90-dF-8d"
}
}
]);
you can use $elemMatch to find a particular object in an array.
Company.find({ configuration: { $elemMatch: { macAddress: "AB-90-dF-8d"} } } );
can you show me your array of objects?

MongoDB - is there a way to $inc ALL elements in an array with one command? [duplicate]

This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 4 years ago.
If I have a collection mycoll of documents like this :
{
vals : [10, 11]
}
where the length of the vals array is NOT fixed.
is there a way to $inc ALL the elements in the vals array with one command ?
I've tried
db.mycoll.update({}, { $inc : { vals : 5 }}, { multi : true })
// error cannot $inc non-number
db.mycoll.update({}, { $inc : { vals.$ : 5 }}, { multi : true })
// error must specify a field
and the goal is to get
{
vals : [15, 16]
}
without having to fetch each document, copy and update the array, then save the updated array back to the document...
my thanks in advance for any ideas !
whoops, there is already a JIRA open for this here
https://jira.mongodb.org/browse/SERVER-1243
You can include multiple fields in your $inc, so while you need to explicitly reference each element by its index, you can do this as:
db.mycoll.update({}, {$inc: {'vals.0': 5, 'vals.1': 5}})

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})