MongoDB updating array of objects does not work as expected - node.js

Got multiple documents in a db one of which looks like this:
{
"searchWord":[
"pizz",
"pizza"
],
"result":[
{
"idMeal":1,
"strMeal":"test1",
"strInstructions":"test1"
},
{
"idMeal":2,
"strMeal":"test2",
"strInstructions":"test2"
}
]
}
Tried to solve it like this:
eg:
db.meals.updateOne(
{
"searchWord": "pizz",
"result": { $elemMatch: { idMeal: "1" } }
},
{ $set: { 'result.$.strMeal' : "UPDATED" } }
)
This doesnt update the respective subdocument only the 2nd as if I wrote
{ $set: { 'result.1.strMeal' : "UPDATED" } }
(Which will result in the 2nd subdocument being updated)
This is the other idea (Same result)
db.meals.updateOne(
{ searchWord: "pizz", 'result.idMeal': 319012 },
{ $set: { "result.$.strMeal" : "fsdf" } }
)
What I dont seem to understand is its exactly the syntax that is provided by mongo yet it doesnt work
The "$" operator doesnt pick up which array of objects I wanna update

Try to use $[] in your $set for multiple positional element
db.collection.update({
"searchWord": "pizz"
},
{
$set: {
"result.$[r].strMeal": "UPDATED"
}
},
{
arrayFilters: [
{
"r.idMeal": 1
}
]
})
Here is the Mongo playground for your reference.

Related

Update nested object in array MongoDB

I need to find and update documents with category that corresponding to the query. Array could contain mo than one corresponding id.
Query:
{
"ids": ["61f1cda47018c60012b3dd01", "61f1cdb87018c60012b3dd07"],
"userId": "61eab3e57018c60012b3db3f"
}
I got collection with documents like:
`{
"_id":{"$oid":"61f1cdd07018c60012b3dd09"},
"expenses":[
{"category":"61eafc104b88e154caa58616","price":"1111.00"},
{"category":"61f1cdb87018c60012b3dd07","price":"2222.00"},
{"category":"61f1cda47018c60012b3dd01","price":"1241.00"},
{"category":"61f1cdb87018c60012b3dd07","price":"111.00"}
],
"userId":"61eab3e57018c60012b3db3f"
}`
my method:
async myMethod(ids: [string], userId: string) {
try {
const { ok } = await this.ExpensesModel.updateMany(
{"userId": userId, "expenses.category": { $in: ids }},
{$set: {"expenses.$.category": "newCategoryID"}}
);
return ok
} ........
I path array of ids ["61f1cda47018c60012b3dd01","61f1cdb87018c60012b3dd07","61f1cdb87018c60012b3dd07"] and userId, this code update only 1 category by document.
So can i made it with mongo build in methods? or i need to find matching document and update it it by my self and after that update or insert;
Update with arrayFilters
db.collection.update({
"expenses.category": {
$in: [
"61f1cda47018c60012b3dd01",
"61f1cdb87018c60012b3dd07"
]
}
},
{
$set: {
"expenses.$[elem].category": "61eab3e57018c60012b3db3f"
}
},
{
arrayFilters: [
{
"elem.category": {
$in: [
"61f1cda47018c60012b3dd01",
"61f1cdb87018c60012b3dd07"
]
}
}
]
})
mongoplayground

Mongoose - Query deeply nested Objects

I currently have a problem where I have to update entries in a deeply nested Document. Now to simplify my problem I have this example. Let's assume I store cars in my MongoDB. A Document would look like this
{
Make: "BMW",
Model: "3Series",
Wheels: [
{
_id: someObjectId
Size: "19 inch",
Screws: [
{
_id: someObjectId
Type : "M15x40"
},
{
_id: someObjectId
Type : "M15x40"
}
]
}
]
}
Now if I want to update a specific Wheel, my code would look somewhat like this
CarModel.findOneAndUpdate({
"_id": CarId, "Wheels._id": WheelId
}, {
"$set" : {
"Wheels.$.Size": NewSize
}
})
Now this works. But I am pretty lost on how I would update an specific screw as I am going through 2 Arrays. Any Idea how I could make this work?
You need arrayFilters functionality to define the path for more than one nested array:
CarModel.findOneAndUpdate(
{ "_id": CarId },
{ $set: { "Wheels.$[wheel].Screws.$[screw].Type": "something" } },
{ arrayFilters: [ { 'wheel._id': WheelId }, { 'screw._id': screwId } ] })

Remove element from nested array mongodb

i have the following document , it has two array's , one inside the other ,
attachment array and files array inside attachment array .
i want to delete an element inside files array using this element _id . but its not working with me , i tried this code , it return
{ n: 144, nModified: 0, ok: 1 }
Invoice.update({}, {
$pull: {
"attachment":
{
"files":
{
$elemMatch:
{ _id: ObjectId("5b7937014b2a961d082de9bf") }
}
}
}
}, { multi: true })
.then(result => {
console.log("delete", result);
});
this is how the document looks like
You can try below update query in 3.6 version.
Invoice.update(
{},
{"$pull":{"attachment.$[].files":{_id:ObjectId("5b7969ac8fb15f3e5c8e844e")}}},
{"multi": true}, function (err, result) {console.log(result);
});
Use db.adminCommand( { setFeatureCompatibilityVersion: 3.6 or 4.0 depending on your version } ) if your are upgrading from old version.
For Mongodb version prior to 3.6
There is only one nested level here so you can simply use $ positional operator.
Invoice.update(
{ "attachment.files._id": mongoose.Types.ObjectId("5b7937014b2a961d082de9bf") },
{ "$pull": { "attachment.$.files": { "_id": mongoose.Types.ObjectId("5b7937014b2a961d082de9bf") }}},
{ "multi": true }
)
For Mongodb version 3.6 and above
If you want to update multiple elements inside attachement array then you can use $[] the all positional operator.
const mongoose = require("mongoose")
Invoice.update(
{ "attachment.files._id": mongoose.Types.ObjectId("5b7937014b2a961d082de9bf") },
{ "$pull": { "attachment.$[].files": { "_id": mongoose.Types.ObjectId("5b7937014b2a961d082de9bf") }}},
{ "multi": true }
)
And If you want to update single element inside the attachment array then you can use $[<identifier>] that identifies the array elements that match the arrayFilters conditions.
Suppose you want to update only an element inside attachment having _id equal to ObjectId(5b7934f54b2a961d081de9ab)
Invoice.update(
{ "attachment.files._id": mongoose.Types.ObjectId("5b7937014b2a961d082de9bf") },
{ "$pull": { "attachment.$[item].files": { "_id": mongoose.Types.ObjectId("5b7937014b2a961d082de9bf") } } },
{ "arrayFilters": [{ "item._id": mongoose.Types.ObjectId("5b7934f54b2a961d081de9ab") }], "multi": true }
)

Conflict when updating a document with both $set and $currentDate

I’m using Mongo 3.6.3 and I have a database with a collection and an item with _id equal to 1.
I want to update the item by adding an object and a timestamp inside of that object. However, I get an error. Here’s what I do:
function MyObject() {
this.bar = {
apples: 4,
bananas: 5
};
}
collection.update({
_id: 1
}, {
$set: {
"foo": new MyObject()
},
$currentDate: {
"foo.time": {
$type: 'timestamp'
}
}
}, function (err) {
console.log(err.name, err.message);
});
and I get:
MongoError Updating the path 'foo.time' would create a conflict at 'foo'
Why does that happen?
If I run the $set operation first and then the $currentDate one in another update(), I get the desired result:
{
"_id" : 1,
"foo" : {
"bar" : {
"apples" : 4,
"bananas" : 5
},
"time" : Timestamp(1523459420, 1)
}
}
However, if I try to do them simultaneously like I’ve shown in the code above, I get the error. Why?
You can't have multiple operators ($set and $currentDate) that modify the same path (foo in this case). In your case you could use dot notation though:
collection.update({
_id: 1
}, {
$set: {
"foo.bar.apples": 4,
"foo.bar.bananas": 5
},
$currentDate: {
"foo.time": {
$type: 'timestamp'
}
}
}, function (err) {
console.log(err.name, err.message);
});
or just change MyObject to set this.time = new Date() instead of using $currentDate.

MongoDB: Regroup by two field with different value

I created a nodejs app in Typescript.
I want to group documents by two fields "one_id" and "two_id" with specified one_id value.
Here is my data in my collection:
{
"_id":"5a8b2953007a1922f00124fd",
"one_id":"307973260186877954",
"two_id":"415228402765660181"
}
{
"_id":"5a8b29a3007a1922f00124fe",
"one_id":"415228402765660181",
"two_id":"307973260186877954"
}
{
"_id":"5a8c119bf6ba49302c3ef67e",
"one_id":"394199132195127306",
"two_id":"270131587092316161"
}
{
"_id":"5a8c11a4f6ba49302c3ef67f",
"one_id":"270131587092316161",
"two_id":"394199132195127306"
}
{
"_id":"5a8c33132a182308a836bc1c",
"one_id":"307973260186877954",
"two_id":"397036401075552256"
}
{
"_id":"5a8c33242a182308a836bc1d",
"one_id":"397036401075552256",
"two_id":"307973260186877954"
}
And if I want to get pairs with one_id="307973260186877954", the excepted result would be: (must have another document with "inversed" fields content)
{
"_id":"5a8b2953007a1922f00124fd",
"one_id":"307973260186877954",
"two_id":"415228402765660181"
}
{
"_id":"5a8b29a3007a1922f00124fe",
"one_id":"415228402765660181",
"two_id":"307973260186877954"
}
{
"_id":"5a8c33132a182308a836bc1c",
"one_id":"307973260186877954",
"two_id":"397036401075552256"
}
{
"_id":"5a8c33242a182308a836bc1d",
"one_id":"397036401075552256",
"two_id":"307973260186877954"
}
I don't know if you understand me.
Thank you, I hope someone will understand me!
You can use $lookup to self join the rows and output the document when there is a match and $project with exclusion to drop the joined field.
db.col.aggregate([
{"$lookup":{
"from":col,
"localField":"one_id",
"foreignField":"two_id",
"as":"onetwo"
}},
{"$lookup":{
"from":col,
"localField":"two_id",
"foreignField":"one_id",
"as":"twoone"
}},
{"$match":{"onetwo.0":{"$exists":true}, "twoone.0":{"$exists":true}}},
{"$project":{"onetwo":0,"twoone":0}}
])
I think this should be your solution. Not sure. Just gave it a try
db.TempCollections.aggregate([
{
$lookup:{
from:"TempCollections",
let:{
leftone_id:"$one_id",
lefttwo_id:"$two_id"
},
pipeline:[
{
$match: {
$expr: {
$and: [
{
$eq: [
"$one_id",
"$$lefttwo_id"
]
},
{
$eq: [
"$two_id",
"$$leftone_id"
]
}
]
}
}
}
],
as:"Final"
}
}
],
{
allowDiskUse:true
});

Resources