Update nested array field for multiple docs at once - arrays

I have a huge number of records in the collection on the below structure.
Here I want to update all floor fields as an empty string "", wherever it's "n/a". It should not affect other blocks which already have value for the floor like the First, Second Floor.
Can someone help on this?
{
"id" : "181",
"EID" : "83",
"History" : [
{
"aNum" : "12324",
"dev" : [
{
"type" : "",
"room" : "Office",
"floor" : "Second Floor"
},
{
"type" : "",
"room" : "Bedroom",
"floor" : "n/a"
},
{
"type" : "",
"room" : "Bedroom",
"floor" : "First Floor"
},
{
"type" : "",
"room" : "Bedroom",
"floor" : "n/a"
},
]
}
]
}

With arrayFilters and filtered $[<identifier>] operator.
db.collection.update({},
{
$set: {
"History.$[].dev.$[dev].floor": ""
}
},
{
arrayFilters: [
{
"dev.floor": "n/a"
}
],
multi: true
})
Sample Mongo Playground

Related

Update array value for multiple documents in mongo db

"ParentType" : {
"Food" : null,
"Inventory" : [
{
"InvId":"5ea11c6569fdbb3728133c4c"
"Unit" : "",
"Price" :5.0
},
{
"InvId":"5ea11c6569fdbb3728133c4d"
"Unit" : "Liter",
"Price" :10.0
},
{
"InvId":"5ea11c6569fdbb3728133c4e"
"Unit" : "",
"Price" :12.0
}
]
}
I need to update Unit "" to Number for all elements in array that matches the criteria.
I tried writing a query as below but it doesn't seem to work right. Pls let me know the correct mongo db script
db.Inv.update(
{ ParentType.Inventory.$[element].Unit: ""},
{ $set: {"ParentType.Inventory.$[element].Unit": "Number"} },
{ arrayFilters: [{"element.Unit": ""}] }
);

Update array at specific index by other filed in MongoDB

I have a collection, consist of name and data.
data is an array with 2 elements, each element is the object with code and qty.
{
"_id" : ObjectId("605c666a15d2612ed0afedd2"),
"name" : "Anna",
"data" : [
{
"code" : "a",
"qty" : 3
},
{
"code" : "b",
"qty" : 4
}
]
},
{
"_id" : ObjectId("605c666a15d2612ed0afedd3"),
"name" : "James",
"data" : [
{
"code" : "c",
"qty" : 5
},
{
"code" : "d",
"qty" : 6
}
]
}
I want to update the code of the first element to name of its document. The result I want is
{
"_id" : ObjectId("605c666a15d2612ed0afedd2"),
"name" : "Anna",
"data" : [
{
"code" : "Anna",
"qty" : 3
},
{
"code" : "b",
"qty" : 4
}
]
},
{
"_id" : ObjectId("605c666a15d2612ed0afedd3"),
"name" : "James",
"data" : [
{
"code" : "James",
"qty" : 5
},
{
"code" : "d",
"qty" : 6
}
]
}
I just google to find how to:
update array at a specific index (https://stackoverflow.com/a/34177929/11738185)
db.Collection.updateMany(
{ },
{
$set:{
'data.0.code': '$name'
}
}
)
But the code of the first element in data array is a string '$name', not a value (Anna, James)
{
"_id" : ObjectId("605c666a15d2612ed0afedd2"),
"name" : "Anna",
"data" : [
{
"code" : "$name",
"qty" : 3
},
{
"code" : "b",
"qty" : 4
}
]
},
{
"_id" : ObjectId("605c666a15d2612ed0afedd3"),
"name" : "James",
"data" : [
{
"code" : "$name",
"qty" : 5
},
{
"code" : "d",
"qty" : 6
}
]
}
update a field by the value of another field. It takes me to use pipeline updating (https://stackoverflow.com/a/37280419/11738185): the second param of updateMany is array (pipeline)
db.Collection.updateMany(
{ },
[{
$set:{
'data.0.code': '$name'
}
}]
)
and It adds field 0 to each element in data array
{
"_id" : ObjectId("605c666a15d2612ed0afedd2"),
"name" : "Anna",
"data" : [
{
"0" : {
"code" : "Anna"
},
"code" : "a",
"qty" : 3
},
{
"0" : {
"code" : "Anna"
},
"code" : "b",
"qty" : 4
}
]
},
{
"_id" : ObjectId("605c666a15d2612ed0afedd3"),
"name" : "James",
"data" : [
{
"0" : {
"code" : "James"
},
"code" : "c",
"qty" : 5
},
{
"0" : {
"code" : "James"
},
"code" : "d",
"qty" : 6
}
]
}
I can't find the solution for this case. Could anyone to help me? How can I update array at fixed index by other field. Thanks for reading!
1. update array at a specific index
You can't use internal fields as value of another fields, it will work only when you have external value to update like { $set: { "data.0.code": "Anna" } }.
2. update a field by the value of another field
Update with Aggregation pipeline can't allow to access data.0.code syntax.
You can try using $reduce in update with aggregation pipeline,
$reduce to iterate loop of data array, set empty array in initialValue of reduce, Check condition if initialValue array size is zero then replace code with name and merge with current object using $mergeObjects, else return current object,
$concatArrays to concat current object with initialValue array
db.collection.update({},
[{
$set: {
data: {
$reduce: {
input: "$data",
initialValue: [],
in: {
$concatArrays: [
"$$value",
[
{
$cond: [
{ $eq: [{ $size: "$$value" }, 0] },
{ $mergeObjects: ["$$this", { code: "$name" }] },
"$$this"
]
}
]
]
}
}
}
}
}],
{ multi: true }
)
Playground
I think easier would be another way.
Just save the model before and use it for updating after
var annaModel = nameModel.findOne({_id: "605c666a15d2612ed0afedd2" })
nameModel.findOneAndUpdate({_id: "605c666a15d2612ed0afedd2"},{$set:{'data.0.code': annaModel.name}})

How to remove a document inside an array in mongodb using $pull

I have the following document structure
{
"_id" : ObjectId("5ffef283f1f06ff8524aa2c2"),
"applicationName" : "TestApp",
"pName" : "",
"environments" : [],
"stages" : [],
"createdAt" : ISODate("2021-01-15T09:51:35.546Z"),
"workflows" : [
[
{
"pName" : "Test1",
"wName" : "TestApp_Test1",
"agent" : ""
},
{
"pName" : "Test2",
"wName" : "TestApp_Test2",
"agent" : ""
}
],
[
{
"pName" : "Test1",
"wName" : "TestApp_Test1",
"agent" : ""
}
]
],
"updatedAt" : Date(-62135596800000)
}
I wish to remove the occurrences of
{
"pName" : "Test1",
"wName" : "TestApp_Test1",
"agent" : ""
}
The resultant document should look like
{
"_id" : ObjectId("5ffef283f1f06ff8524aa2c2"),
"applicationName" : "TestApp",
"pName" : "",
"environments" : [],
"stages" : [],
"createdAt" : ISODate("2021-01-15T09:51:35.546Z"),
"workflows" : [
[
{
"pName" : "Test2",
"wName" : "TestApp_Test2",
"agent" : ""
}
]
],
"updatedAt" : Date(-62135596800000)
}
I've tried the below mongo query
db.getCollection('workflows').update({_id:ObjectId('5ffef283f1f06ff8524aa2c2')},
{$pull:{workflows: { $elemMatch: {pipelineName: 'Test1'}}}} )
This is removing all the documents from workflows field including Test2 since Test1 is matched.
How can we remove only the entries for Test1 and keep the others?
You can do it using the positional operator "$[]" :
db.getCollection('workflows').update({_id: ObjectId("5ffef283f1f06ff8524aa2c2") }, {$pull: {"workflows.$[]":{pName:"Test1" } } } )
but the schema looks abit strange and after the update you will have empty arrays inside workflows if all elements got deleted in the sub-array.
To fix the empty sub-arrays you will need to perform second operation to remove them:
db.getCollection('workflows').update({_id: ObjectId("5ffef283f1f06ff8524aa2c2") }, {$pull: {"workflows":[] } } )
You cannot use $elemMatch as it returns the first matching element in the array.
I am not sure there is another best way to do this with the provided schema design.
play
db.collection.aggregate({
"$unwind": "$workflows"
},
{
"$unwind": "$workflows"
},
{
"$match": {
"workflows.pName": {
"$ne": "Test1"
}
}
},
{
"$group": {
"_id": "$_id",
workflows: {
$push: "$workflows"
},
applicationName: {
"$first": "$applicationName"
}
}
},
{
"$group": {
"_id": "$_id",
workflows: {
$push: "$workflows"
},
applicationName: {
"$first": "$applicationName"
}
}
})
unwind twice required to de-normalize the data
match to filter out the unnecessary doc
group twice required to bring the required output
You can save this to a collection using $out as last stage.

Update created a conflict on multiple arrayFilters

I'm trying to increment two different arrays in a similar fashion. Without incrementing elementC, everything works fine. If I add the elementC and use the arrayFilter on it, I get the error: update created a conflict at tasks.priority. I think this has something to do with iterating over the same object, but why and how would I solve this? My code:
const project = await Project.updateOne(
{_id: req.params.project_id},
{
$set: {'tasks.$[elementA].priority': req.body.new_state.priority, 'tasks.$[elementA].state': req.body.new_state.state},
$inc: {'tasks.$[elementB].priority': 1, 'tasks.$[elementC].priority': 2},
},
{
arrayFilters: [
{'elementA._id': req.params.task_id},
{'elementB.state': req.body.old_state.state, 'elementB.priority': {$gt: req.body.old_state.priority}},
{'elementC.state': req.body.new_state.state, 'elementC.priority': {$gt: req.body.new_state.priority}}
]
}
);
res.json(project);
schema:
const task_schema = new mongoose.Schema({
description: {
type: String
},
state: {
type: String
},
priority: {
type: Number
}
const project_schema = new mongoose.Schema({
name: {
type: String
},
description: {
type: String
},
non_public: {
type: Boolean
},
tasks: [task_schema]
Example:
{ "_id" : ObjectId("5ec274b50877c671ccf9b6c7"),
"name" : "first project",
"description" : "lorem ipsum",
"non_public" : true,
"tasks" : [ { "_id" : ObjectId("5ec27674e0db347307902ba9"),
"description" : "Lorem",
"state" : "TODO",
"priority" : 0 },
{ "_id" : ObjectId("5ec27686e0db347307902baa"),
"description" : "Ipsum",
"state" : "TODO",
"priority" : 1 },
{ "_id" : ObjectId("5ec27686e0db347307902baa"),
"description" : "Dolor",
"state" : "TODO",
"priority" : 2 },
{ "_id" : ObjectId("5ec293db751ae07a8520a703"),
"description" : "Sit",
"state" : "DOING",
"priority" : 0 },
{ "_id" : ObjectId("5ec29591ad84927c7788116d"),
"description" : "Amet",
"state" : "DONE",
"priority" : 0 },
]}
This is happening because you are trying to apply two different filters on the same array field and to update it. So, the Mongo engine is getting confused on which filter to apply.
Hint: Your arayFilters have to be mutually exclusive.

MongoDB object property $exists in nested array

I have a folowing object structure in my db collection:
{
"name" : "test",
"code" : "test",
"attributes" : [
{
"name" : "test1",
"code" : "code1"
},
{
"name" : "test2",
"code" : "code2",
"value" : true
},
{
"name" : "test3",
"code" : "code3",
"value" : ""
},
{
"name" : "test4",
"code" : "code4"
"value" : [
{
"code" : "code4.1",
"name" : "test4.1"
},
{
"name" : "test4.2"
}
]
}
]
}
So "value" property can be empty string, boolean, array or even not defined at all.
How can I make query to list objects that have non-empty attributes array and don't have "attributes.value" property defined inside at least one object inside array?
p.s. I tried following query:
db.collection.find({"attributes": {$exists: true, $ne: []}, "attributes.value": {$exists: false}})
but query result is empty.
The $elemMatch operator matches documents that contain an array field
with at least one element that matches all the specified query
criteria.
This query work for me:
db.getCollection('testeur').find({ "attributes": {
$exists: true,
$ne: [],
$elemMatch: { "value": {$exists: false } }
}
})

Resources