How to rename all the key in mongodb? - 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": "" } }
)

Related

MongoDB - Update an Key

I have been trying to update an Object for this collection. Below is the collection. Looking for Server 3.6 version.
Here The ask is Need to update the class name from "HISTORY" to " HISTORY_NEW". Need to do, for some students in the class. Need a query that will select all student records in student collection with "HISTORY" class in it and update them to "HISTORY_NEW ". I have around 30,000 records and not getting a bulk update method.
{
"_id" : ObjectId("611f90aa43f77a728879c395"),
"studentId" : "stu1",
"classes" : {
"History" : {
"TeacherName" : "T1",
"Marks" : [
{
"Internal": 15
}
]
},
"Geography" : {
"TeacherName" : "T2",
"Marks" : [
{
"Internal" : 20
}
]
}
},
"updateDate" : ISODate("2021-10-12T11:40:47.156Z")
}
This is the result I am expecting
{
"_id" : ObjectId("611f90aa43f77a728879c395"),
"studentId" : "stu1",
"classes" : {
"HISTORY_NEW" : {
"TeacherName" : "T1",
"Marks" : [
{
"Internal": 15
}
]
},
"Geography" : {
"TeacherName" : "T2",
"Marks" : [
{
"Internal" : 20
}
]
}
},
"updateDate" : ISODate("2021-10-12T11:40:47.156Z")
}
.Or is that even possible with the kind of collection above or going via code route?
So far this is what I have, without any success.
Get all students' Ids and then update the Class name. But that is also not working and don't think it is smart to update DB 30,000 times.
var studentIds =[];
db.studentSubject.find({"classes.History":{$exists:true}})
.forEach(function(u) { studentIds.push(u.studentId) })
studentIds.forEach(function(studentId) {
var result;
try {
result =db.studentSubject.updateOne(
{studentId:studentId},
{ $set : {"classes.History": "HISTORY_NEW",}},
{ upsert: false});
} catch (e) {
print(e);
}
});
From your scenario, you need $rename operator.
As discussed in the comment, you don't need to fetch each document to get studentId and then pass it to update each document. Just bulk update by checking the document has classes.History field.
db.collection.update({
"classes.History": {
$exists: true
}
},
{
$rename: {
"classes.History": "classes.HISTORY_NEW"
}
},
{
upsert: false,
multi: true
})
Sample Mongo Playground

mongo updateMany $set nested array when some docs do not contain the path

I have a seemingly straight forward query to update the names of like objects in a mongo doc. But it fails as not all docs have comment.. what should i be looking for in the docs get around this and update only the docs that have likes on comments?
return this.model.updateMany(
{
comments: {
$exists: true
}
},
{
$set: {
'comments.likes.$[like].actor.firstName': user.firstName,
'comments.likes.$[like].actor.lastName': user.lastName,
},
},
{
multi: true,
arrayFilters: [
{ 'like.actor.username': user.username },
],
},
);
The problem is that not all comments are liked.. so then the query complains:
WriteError: The path 'comments.likes' must exist in the document in order to apply array updates.
Full output:
WriteError: The path 'comments.likes' must exist in the document in order to apply array updates.
Details:
WriteError({
"index" : 0,
"code" : 2,
"errmsg" : "The path 'comments.likes' must exist in the document in order to apply array updates.",
"op" : {
"q" : {
"comments" : {
"$exists" : true
}
},
"u" : {
"$set" : {
"comments.likes.$[like].actor.firstName" : "GGGJOHN",
"comments.likes.$[like].actor.lastName" : "Carmichael"
}
},
"multi" : true,
"upsert" : false,
"arrayFilters" : [
{
"like.actor.username" : "john.carmichael"
}
]
}
})
WriteError#src/mongo/shell/bulk_api.js:461:48
Bulk/mergeBatchResults#src/mongo/shell/bulk_api.js:841:49
Bulk/executeBatch#src/mongo/shell/bulk_api.js:906:13
Bulk/this.execute#src/mongo/shell/bulk_api.js:1150:21
DBCollection.prototype.updateMany#src/mongo/shell/crud_api.js:655:17
#(shell):1:1
Here is an illustration of the doc in q, where comments is an array and the likes on a single comment is also an array:
what if you added
under match part of the update part query you are checking if likes attribute exists in the comments object
return this.model.updateMany(
{
"comments.likes": { // <---
$exists: true
}
},
{
$set: {
'comments.likes.$[like].actor.firstName': user.firstName,
'comments.likes.$[like].actor.lastName': user.lastName,
},
},
{
multi: true,
arrayFilters: [
{ 'like.actor.username': user.username },
],
},
);

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>, ... } }

get a mongo document based in two different values

I have the following document structure
{
"_id" : "aaa0001",
"path" : "/some/path",
"information" : {
"name" : "info"
},
"colors" : {
"colors" : [
{
"key" : "AAAA001",
"name" : "White"
},
{
"key" : "BBBB002",
"name" : "Black"
}
]
}
}
the idea is that I have to return the document by the color key. I have two parameters the "path" and the "color", so, I was trying to make something like this
db.components.find(
{$and:[
{"path" : "/some/path"},
{"colors":{"colors" : {$elemMatch: { "key" : "AAAA001" } } } }
]})
I'm getting the following message "Script is executed successfully, but there is no results to show".
Can anyone give me some directions regarding this?
thanks
Use the following query:
db.components.find({
"path": "/some/path",
"colors.colors.key" : "AAAA001"
})
MongoDB expects query document to contain field-value pairs { <field>: <value> }. So, in your example you're querying for a document with colors field equal to:
{"colors" : {$elemMatch: { "key" : "AAAA001" } } }
As for $and and $elemMatch operators, you don't need them in such a simple query.
For more information read Query Documents.
Update
You can also select only matching subdocument from colors array using Positional Operator $:
db.components.find({
"path": "/some/path",
"colors.colors.key" : "AAAA001"
}, {
_id: 0,
"colors.colors.$": 1
})
Though, you won't be able to change your documents structure, thus getting
{ "colors" : { "colors" : [ { "key" : "AAAA001", "name" : "White" } ] } }

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.