Concatenate array with existing array in document [duplicate] - mongodb

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/

Related

Mongoose: How to return array of Object _ids within aggregation pipeline [duplicate]

This question already has answers here:
Return result as an Array of Values Only
(2 answers)
Closed 3 years ago.
I can format my response into this, an array of objects of _ids:
[ { "_id": "5d703c09af11414e538fe95b" }, { "_id": "5d704b9b1ba3f75232d778d4" }, { "_id": "5d704bbe1ba3f75232d779de" } ]
However is it possible, within the aggregation pipeline, to transform it into just an array of the Object ids:
["5d703c09af11414e538fe95b", "5d704b9b1ba3f75232d778d4", "5d704bbe1ba3f75232d779de"]
I am aware that I could simply take the original response and map over it but I am unsure if we could do this directly within mongooose.
use $group operator as
{$group:{_id:null,ids:{$push:"$_id"}}}
this will return an array of ids

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

updating multiple documents by passing a list objectids to findByIdAndUpdate [duplicate]

This question already has answers here:
MongoDB: How to update multiple documents with a single command?
(13 answers)
FindAndModify, return array of Objects
(1 answer)
Closed 5 years ago.
I am trying to use mongoose's findByIdAndUpdate method to pass in a list of object id's and updating them at once. However, I am getting a "Error: Can't use $set with ObjectId." error which I can't seem to associate with my code.
Here's my code.
return ComponentModel.findByIdAndUpdate({
_id: {
$in: input.subjectIds
},
$set: { location: input.newLocation }
}).then(res => res);
findByIdAndUpdate is for one document. For multiple documents you can use update with multi flag true.
return ComponentModel.update(
{_id: {$in: input.subjectIds}},
{$set: {location: input.newLocation}},
{"multi": true}
).then(res => res);

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?

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