MongoDB - How to add a field to all object within an array - mongodb

I have a document with a field called info, and info has a field inside it called data. data is an array of objects. I want to add a new boolean field, isActive: false, to each object in data, with updateMany.
This is how it looks now
{
info: {
data: [{
"name": "Max"
},
{
"name": "Brian"
},
...
]
}
}
This is what I want:
{
info: {
data: [{
"name": "Max",
"isActive": false
},
{
"name": "Brian",
"isActive": false
},
...
]
}
}
How do I do that?

Add the isActive field with all positional operator $[].
db.collection.update({},
{
$set: {
"info.data.$[].isActive": false
}
},
{
multi: true
})
Consider applying { multi: true } if you want to update multiple documents.

Related

MongoDB - Update the value of one field with the value of another nested field

I am trying to run a MongoDB query to update the value of one field with the value of another nested field. I have the following document:
{
"name": "name",
"address": "address",
"times": 10,
"snapshots": [
{
"dayTotal": 2,
"dayHit": 2,
"dayIndex": 2
},
{
"dayTotal": 3,
"dayHit": 3,
"dayIndex": 3
}
]
}
I am trying like this:
db.netGraphMetadataDTO.updateMany(
{ },
[{ $set: { times: "$snapshots.$[elem].dayTotal" } }],
{
arrayFilters: [{"elem.dayIndex":{"$eq": 2}}],
upsert: false,
multi: true
}
);
but got an error:
arrayFilters may not be specified for pipeline-syle updates
You can't use arrayFilters with aggregation pipeline for update query at the same time.
Instead, what you need to do:
Get the dayTotal field from the result 2.
Take the first matched document from the result 3.
Filter the document from snapshots array.
db.netGraphMetadataDTO.updateMany({},
[
{
$set: {
times: {
$getField: {
field: "dayTotal",
input: {
$first: {
$filter: {
input: "$snapshots",
cond: {
$eq: [
"$$this.dayIndex",
2
]
}
}
}
}
}
}
}
}
],
{
upsert: false,
multi: true
})
Demo # Mongo Playground

How to avoid adding duplicate objects to an array in MongoDB

this is my schema:
new Schema({
code: { type: String },
toy_array: [
{
date:{
type:Date(),
default: new Date()
}
toy:{ type:String }
]
}
this is my db:
{
"code": "Toystore A",
"toy_array": [
{
_id:"xxxxx", // automatic
"toy": "buzz"
},
{
_id:"xxxxx", // automatic
"toy": "pope"
}
]
},
{
"code": "Toystore B",
"toy_array": [
{
_id:"xxxxx", // automatic
"toy": "jessie"
}
]
}
I am trying to update an object. In this case I want to update the document with code: 'ToystoreA' and add an array of subdocuments to the array named toy_array if the toys does not exists in the array.
for example if I try to do this:
db.mydb.findOneAndUpdate({
code: 'ToystoreA,
/*toy_array: {
$not: {
$elemMatch: {
toy: [{"toy":'woddy'},{"toy":"buzz"}],
},
},
},*/
},
{
$addToSet: {
toy_array: {
$each: [{"toy":'woddy'},{"toy":"buzz"}],
},
},
},
{
new: false,
}
})
they are added and is what I want to avoid.
how can I do it?
[
{
"code": "Toystore A",
"toy_array": [
{
"toy": "buzz"
},
{
"toy": "pope"
}
]
},
{
"code": "Toystore B",
"toy_array": [
{
"toy": "jessie"
}
]
}
]
In this example [{"toy":'woddy'},{"toy":"buzz"}] it should only be added 'woddy' because 'buzz' is already in the array.
Note:when I insert a new toy an insertion date is also inserted, in addition to an _id (it is normal for me).
As you're using $addToSet on an object it's failing for your use case for a reason :
Let's say if your document look like this :
{
_id: 123, // automatically generated
"toy": "buzz"
},
{
_id: 456, // automatically generated
"toy": "pope"
}
and input is :
[{_id: 789, "toy":'woddy'},{_id: 098, "toy":"buzz"}]
Here while comparing two objects {_id: 098, "toy":"buzz"} & {_id: 123, "toy":"buzz"} - $addToSet consider these are different and you can't use $addToSet on a field (toy) in an object. So try below query on MongoDB version >= 4.2.
Query :
db.collection.updateOne({"_id" : "Toystore A"},[{
$addFields: {
toy_array: {
$reduce: {
input: inputArrayOfObjects,
initialValue: "$toy_array", // taking existing `toy_array` as initial value
in: {
$cond: [
{ $in: [ "$$this.toy", "$toy_array.toy" ] }, // check if each new toy exists in existing arrays of toys
"$$value", // If yes, just return accumulator array
{ $concatArrays: [ [ "$$this" ], "$$value" ] } // If No, push new toy object into accumulator
]
}
}
}
}
}])
Test : aggregation pipeline test url : mongoplayground
Ref : $reduce
Note :
You don't need to mention { new: false } as .findOneAndUpdate() return old doc by default, if you need new one then you've to do { new: true }. Also if anyone can get rid of _id's from schema of array objects then you can just use $addToSet as OP was doing earlier (Assume if _id is only unique field), check this stop-mongoose-from-creating-id-property-for-sub-document-array-items.

Mongodb Update Elements from Multiple Arrays on a Document

I'd like to update a property from objects located in multiple lists on a MongoDB collection's document.
In a MongoDB collection I have documents like this one:
{
"firstArray": [
{
"name": "john",
"updated": false // I wanna switch that to true
},
{
"name": "bob",
"updated": false
}
],
"secondArray": [
{
"name": "eric",
"updated": false
},
{
"name": "john",
"updated": false // I wanna switch that to true
}
]
}
My goal is to update every element with "name":"john" from document's properties firstArray and secondArray to updated:true.
As they can be parallel accesses to a document, I shall only use update operators (a read and replace method like that one would add the risk of dropping changes made by another process)
It looks like the filtered positional operator $[] as mentioned in that question is the way to go to update an object's property from a document's Array BUT it appears that it doesn't support multiple $set with the same operator nor using two operators (one by array) in two different $set!
As this mongo shell command only update the secondArray:
db['collection'].update(
{},
{
"$set": { "firstArray.$[elem].updated": true },
"$set": { "secondArray.$[elem].updated": true }
},
{ "arrayFilters": [{ "elem.name": "john" }], "multi": true })
And this command returns The array filter for identifier 'a' was not used in the update { $set: { secondArray.$[b].updated: true } }:
db['collection'].update(
{},
{
"$set": { "firstArray.$[a].updated": true },
"$set": { "secondArray.$[b].updated": true }
},
{ "arrayFilters": [{ "a.name": "john" }, { "b.name": "john" }], "multi": true })
So, would you have any idea how to do that update? (Bonus point for any documentation quotes about the above limitations )
Thanks!
The second argument to that function in an object. A given field name can only have one value in an object. To show what is being sent to the server, I assigned the value you are using to a variable:
> var o={
"$set": { "firstArray.$[a].updated": true },
"$set": { "secondArray.$[b].updated": true }
};
> printjson(o)
{ "$set" : { "secondArray.$[b].updated" : true } }
As you can see, the first value was overwritten by the second because both fields were named $set.
Try combining these into a single field:
{
"$set": {
"firstArray.$[elem].updated": true,
"secondArray.$[elem].updated": true
}
}

How to update multi property value of an object in array in MongoDB?

The following is my database schema update operation:
db.school_student.update({ _id: "003" }, {
$set: {
"result": [
{
"_id": "001",
"isPassed": false
},
{
"_id": "002",
"isPassed": false,
},
{
"_id": "003",
"isPassed": false
}
]
}
});
I want to change ALL the property values of "isPassed" to true. Is there any way to update this? I have been struggling with this the whole day :(
db.school_student.update({},{$Set :{"result.isPassed" : true}}
this should update all the documents in school_student collection and set isPassed to true.

Mongo Update $set not working

I'm trying to mass update some mongo documents.
I'm using the query
db.articles.update(
{
'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"),
'source.importer': 'pa'
},
{
$set :
{
'source.expires-at': ISODate("2014-01-01T08:39:45Z")
}
}
)
This query does not update the source.expires-at field, however the where part of the statement works fine.
The document structure is
{
"_id": ObjectId("5211dc100000044707000015"),
"categories": {
"0": {
"id": ObjectId("51cd5272222wb6zs464fa4d9")
}
},
"source": {
"importer": "pa",
"expires-at": ISODate("2013-09-18T08:49:32.0Z")
}
}
Try this:
db.articles.update(
{
'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"),
'source.importer': 'pa'
},
{
$set: {
'source.expires-at': ISODate("2014-01-01T08:39:45Z")
}
},
{ multi: true }
)
You will need to pass additional option argument {multi: true};
Look in to this https://education.10gen.com/courses/10gen/M101JS/2013_August/courseware/CRUD/Multi-update/