How to update a field value in array in mongo database? - mongodb

How can we update a value for 'Enabled' field in Configuration at index 1 in Configurations Array in mongo database ?
Below is my Json data.
{
"Configurations" : [
{
"Configuration" : {
"Host" : "",
"Port" : "1521",
"Enabled" : "true"
}
},
{
"Configuration" : {
"Host" : "",
"Port" : "",
"Enabled" : "true"
}
}
],
"Description" : "Check Database Server"
}
Is there any way to update value for Enabled field in Configuration ??
How can we update a value for 'Enabled' field in Configuration at index 1 in Configurations Array in Mongodba ?
I want to update Enabled field value of 2nd configuration in Configurations Array.

What about something like this
db.collection.update({},
{ "$set": { "Configurations.1.Configuration.Enabled": false }}
)

Try this:
db.collection.update({
"Configurations": {
"$elemMatch": {
"Configuration.Port": "1521"
}
}
}, {
"$set": {
"Configurations.$.Configuration.Enabled": "false"
}
})

Related

ELASTICSEARCH - Update the value of a field to current date

I'm looking for a way to update the value of a field through update_by_query in the field: reviewed_data in the next document:
{
"reviewed_date" : "2022-07-05T09:31:04.742077702Z",
"timestamp" : "2022-07-05T10:18:52.353852943Z"
}
I'm doing it through the following way:
POST index1/_update_by_query
{
"script": {
"source": "ctx._source.reviewed_date = OffsetDateTime.parse(ctx._source.reviewed_date)" ,
"lang": "painless"
},
"query": {
"term": {
"_id": "23r2fvwc3drgr4f2323dsd"
}
}
}
This way I only manage to update the value of the timestamp date, even though I am indicating reviewed_date.
But I would like to be able to assign the value directly to reviewed_data without modifying other fields.
Mapping:
"timestamp" : {
"type" : "date"
}
"reviewed_date" : {
"type" : "date"
},

How to update Meteor array element inside a document

I have a Meteor Mongo document as shown below
{
"_id" : "zFndWBZTvZPgSKXHP",
"activityId" : "aRDABihAYFoAW7jbC",
"activityTitle" : "Test Mongo Document",
"users" : [
{
"id" : "b1#gmail.com",
"type" : "free"
},
{
"id" : "JqKvymryNaCjjKrAR",
"type" : "free"
},
],
}
I want to update a specific array element's email with custom generated id using Meteor query something like the below.
for instance, I want to update the document
if 'users.id' == "b1#gmail.com" then update it to users.id = 'SomeIDXXX'
So updated document should looks like below.
{
"_id" : "zFndWBZTvZPgSKXHP",
"activityId" : "aRDABihAYFoAW7jbC",
"activityTitle" : "Test Mongo Document",
"users" : [
{
"id" : "SomeIDXXX",
"type" : "free"
},
{
"id" : "JqKvymryNaCjjKrAR",
"type" : "free"
},
],
}
I have tried the below but didnt work.
Divisions.update(
{ activityId: activityId, "users.id": emailId },
{ $set: { "users": { id: _id } } }
);
Can someone help me with the relevant Meteor query ? Thanks !
Your query is actually almost right except for a small part where we want to identify the element to be updated by its index.
Divisions.update({
"activityId": "aRDABihAYFoAW7jbC",
"users.id": "b1#gmail.com"
}, {
$set: {"users.$.id": "b2#gmail.com"}
})
You might need the arrayFilters option.
Divisions.update(
{ activityId: activityId },
{ $set: { "users.$[elem].id": "SomeIDXXX" } },
{ arrayFilters: [ { "elem.id": "b1#gmail.com" } ], multi: true }
);
https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/
You need to use the $push operator instead of $set.
{ $push: { <field1>: <value1>, ... } }

How to rename all the key in mongodb?

i have a documents like this :
{
"_id" : ObjectId("5705fe62d0d50b2316617508"),
"date" : ISODate("2016-04-07T11:12:43.917Z"),
}
{
"_id" : ObjectId("5705fe62d0d50b2316617508"),
"date" : ISODate("2016-04-07T11:12:43.917Z"),
"XString" : "bb"
}
{
"_id" : ObjectId("5705fe62d0d50b2316617508"),
"date" : ISODate("2016-04-07T11:12:43.917Z"),
"XString" : "ba"
}
{
"_id" : ObjectId("5705fe62d0d50b2316617508"),
"date" : ISODate("2016-04-07T11:12:43.917Z"),
"XString" : "dd"
}
how i can update key "XString" to "Name" and if the XString is not there it should add "Name"
tried by using $rename as:
db.articles.update({}, {$set:{$rename: {'XString':'Name'}}},{upsert:true});
but it gives error as:
The dollar ($) prefixed field '$rename' in '$rename' is not valid for storage.
Tried other way:
db.articles.update({}, {$rename: {'XString': 'Name'}}, false, true);
but its not adding the key is it does not exists
Wrong option. You want "multi" and not "upsert":
db.articles.update(
{ "XString": { "$exists": true } },
{ "$rename": { 'XString': 'Name' } },
{ "multi": true }
);
The $exists test makes sure you are only selecting documents where the key you are renaming is actually there.
Also $rename is a "update modifier" which means it is a "top level" argument of the "update" document.
The "multi" means to affect all matched documents, rather than just the "first".
If you want a "blank" value where the key does not actually exist then you do the opposite, and preferably after the $rename operation:
db.articles.update(
{ "Name": { "$exists": false } },
{ "$set": { 'Name': '' } },
{ "multi": true }
);
If your MongoDB server version is 3.2 you can use the updateMany() method. Of course you need to use $exists and $rename as already mentioned in this answer. Also the updateMany() method doesn't take the "multi" option.
Last but not least major drivers deprecate the update() method since version 3.0
The following query rename "XString" to "Name" where "XString" exists.
db.articles.updateMany(
{ "XString": { "$exists": true } },
{ "$rename": { "XString": "Name" } }
)
To set default value where the "XString" doesn't exist, use the $set update operator.
db.articles.updateMany(
{ "Name": { "$exists": false } },
{ "$set": { "Name": "" } }
)

changing field in a set object in mongodb

I have this document in mongo:
{
id: objectId,
list: [
{
id: internalObjectId1,
enabled: true
},
{
id: internalObjectId2,
enabled: false
}]
}
I need to change the enabled field. How can I do it?
Use the positional $ operator. Suppose you have the following document in the collection whose list element value is an array of embedded documents:
{
"_id" : ObjectId("551be1a04db8a16ac729432e"),
"list" : [
{
"id" : ObjectId("54f43159c922ac0b4387ef9c"),
"enabled" : true
},
{
"id" : ObjectId("54f43159c922ac0b4387ef9d"),
"enabled" : false
}
]
}
The following will update the value of the enabled field in the embedded document with the id of 54f43159c922ac0b4387ef9d to true:
db.collection.update(
{
"_id": ObjectId("551be1a04db8a16ac729432e"),
"list.id": ObjectId("54f43159c922ac0b4387ef9d")
},
{
"$set": {"list.$.enabled": true}
}
)

Modify a document inside an array in MongoDB

Past answers (from mid 2013 and before) don't seem to work and links to the documentation are all out of date.
Example user object:
{
"name": "Joe Bloggs",
"email": "joebloggs#example.com",
"workstations" : [
{ "number" : "10001",
"nickname" : "home" },
{ "number" : "10002",
"nickname" : "work" },
{ "number" : "10003",
"nickname" : "vacation" }
]
}
How can I modify the nickname of a workstation?
I tried using $set, workstations.$ and workstations.nickname but none gave the desired results.
Short answer, you have to use array index. For example, you want to update the nickname of 10002: {$set:{"workstations.1.nickname":"newnickname"}}
Here is the complete example:
> db.test.update({"_id" : ObjectId("5332b7cf4761549fb7e1e72f")},{$set:{"workstations.1.nickname":"newnickname"}})
> db.test.findOne()
{
"_id" : ObjectId("5332b7cf4761549fb7e1e72f"),
"email" : "joebloggs#example.com",
"name" : "Joe Bloggs",
"workstations" : [
{
"number" : "10001",
"nickname" : "home"
},
{
"nickname" : "newnickname",
"number" : "10002"
},
{
"number" : "10003",
"nickname" : "vacation"
}
]
}
>
If you don't know the index (position of the workstations), you can update the doc using $elemMatch:
>db.test.update(
{
"email": "joebloggs#example.com",
"workstations": { "$elemMatch" { "number" : "10002" } }
},
{
"$set": { "workstations.$.nickname": "newnickname2" }
}
)
>
#naimdjon's answer would work. To generalize, you could use the $elemMatch operator in combination with the $ positional operator to update one element in the array using below query:
db.test.update({
// Find the document where name="Joe Bloggs" and the element in the workstations array where number = "10002"
"name": "Joe Bloggs",
"workstations":{$elemMatch:{"number":"10002"}}
},
{
// Update the nickname in the element matched
$set:{"workstations.$.nickname":"newnickname"}
})
Note: $elemMatch is only required if you need to match more than one component in the array. If you are going to match on just the number, you could use "workstations.number":"10002"
As long as you know "which" entry you wish to update then the positional $ operator can be of help. But you need to update your query form:
db.collection.update(
{
"email": "joebloggs#example.com",
"workstations": { "$elemMatch" { "nickname" : "work" } }
},
{
"$set": { "workstations.$.nickname": "new name" }
}
)
So that is the general form. What you need to do here is "match" something in the array in order to get a "position" to use for the update.
Alternately, where you know the position, then you can just "specify" the position with "dot notation":
db.collection.update(
{
"email": "joebloggs#example.com",
},
{
"$set": { "workstations.1.nickname": "new name" }
}
)
Which updates the second element in the array, and does not need the "matching" part in the query.