Copy Value from One key to another in mongodb? [duplicate] - mongodb

This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 3 years ago.
I am trying to migrate data in mongodb from old schema to new schema in mongodb. where slug_url is added which is exact copy of value of key 'name'. how can i acheive this using mongoshell ?
Previous Data :
{
name:'test',
}
Want
{
name:'test',
slug_url:'test'
}

It can be done using forEach -
db.<collectionName>.find().forEach(function(result)
{
db.<collectionName>.update({"_id" : result._id}, {$set : {"slug_url" : result.name}});
})

Related

Updating column datatype in mongodb [duplicate]

This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 4 years ago.
I am trying to update datatype of a column in mongodb dataset, but even after successful run of command expected changes are not reflecting in compass.
query:
db.EquityPrice.find().forEach(function(data) {
db.EquityPrice.update({_id : doc._id}, {$set : {Trade_Date : new Date(doc.Trade_Date) }});
})
Can anyone please help to figure out where I am going wrong?
db.EquityPrice.find().forEach(function(data) {
db.EquityPrice.update({_id : doc._id}, {$set : {Trade_Date : new Date(doc.Trade_Date) }});
})
In your code you are finding all entries in the collection EquityPrice and forEach element applying a function which has the document (data).
The update function which is called for each document expects a query param as first argument. In your case you pass {_id: doc._id} - but the object docis not defined and therefore undefined. The query doesn't find an element with the _id of undefined.
You want to update the passed data object - therefore change your code to:
db.EquityPrice.update({_id : data['_id']}, {$set : {Trade_Date : new Date(data['Trade_Date']) }});

How to sort array object in mongodb query with pagination? [duplicate]

This question already has answers here:
How to sort array inside collection record in MongoDB?
(16 answers)
mongodb group values by multiple fields
(4 answers)
Update Array Children Sorted Order
(2 answers)
Closed 5 years ago.
I have written mongo query with node.js.
I have array object in my document. I want to apply pagination & sort on that array.
"user_notifications" : [
{
"_id" : ObjectId("59a7c30ecd6109c914c5fb5f"),
"notification_on" : ISODate("2017-09-28T10:00:44.828+0000"),
"procedure_id" : "59ccc84c1312ac3756944736"
},
{
"_id" : ObjectId("59db55ed57983b04534f0285"),
"notification_on" : ISODate("2017-10-09T10:56:45.235+0000"),
"procedure_id" : "59db55ed57983b04534f024d"
},
{
"_id" : ObjectId("59db672b9700ef5f54f25b3a"),
"procedure_id" : "59db672b9700ef5f54f25b0a",
"notification_on" : ISODate("2017-10-09T12:10:19.900+0000")
},
{
"_id" : ObjectId("59db694f89cb7e9954af26ad"),
"notification_on" : ISODate("2017-10-09T12:19:27.954+0000"),
"procedure_id" : "59db694f89cb7e9954af267d"
}
]
I want to sort “user_notification” array using “notification_on” key. I have used following code for pagination which is perfectly working fine.
UserModel.findOne({"_id":user._id},{user_notifications:{$slice:[skip,
limit]}}, function(err , user_obj){
.
.
.
});
But I am unable to sort records.
This question is different than other because I am retrieving only one record which has "user_notifications" object. I wanted to apply pagination for objects in this array & this array needs to sort by notification_on key.
Thanks

Updating Every Record in Mongo [duplicate]

This question already has answers here:
Update MongoDB field using value of another field
(12 answers)
Closed 6 years ago.
How would I update every record in the "title" collection for mongo, to make a field named LastReturnedName set to Name for every single record?
Thought it would be something like this:
db.title.update(
{ "LastNameReturned" : "" },
{ $set:{ "LastNameReturned" : Name } },
{ multi : true }
);
You can use foreach iteration:
db.title.find({ "LastNameReturned" : "" }).snapshot()
.forEach(function(t) {
db.title.update({_id: t._id}, {$set: {"LastNameReturned" : t.Name}});
});
NOTE: You can use snapshot() only with unsharded collections. Also you can speed up updating if you'll use bulk operation.

Mongo query on dynamic fields in embedded document [duplicate]

This question already has answers here:
MongoDB Query Help - query on values of any key in a sub-object
(3 answers)
Closed 6 years ago.
I have the following structure :
{
"FiledA" : "FiledAValue",
"FiledB" : "FiledBValue",
"FiledC" : {
"X" : "XValue",
"Y" : "YValue",
"Z" : "ZValue"
},
}
FiledC content may be dynamic (other fileds than x,y,z)
The user will send a query like {"FiledA" : "12" , "FiledC" : "333"}
The query should match 12 for FiledA and if there any FiledC which one of its filds contains 333
How I can solve the FiledC issue in a query ?
Thanks in advance ...
You should not use dynamic fields to query. It is not possible to query on these fields without individually inspecting all the fields. The similar approach has been exemplified at this answer
If you are open to changing your schema, I would recommend changing it to :
{
FieldC:[
{name:"X", value:"value1"},
{name:"Y", value:"value2"},
{name:"Z", value:"value3"}
]
}
You can now query using :
db.collection.find({"FieldC.value":"testValue"});

Querying key-value map in mongo [duplicate]

This question already has answers here:
Mongodb Query To select records having a given key
(3 answers)
Closed 7 years ago.
I have documents stored in mongo database following this schema:
{
map:{
key1:value,
banana:value2
....
}
}
How can I query objects based on keys in this map ?
e.g I want to get all the documents which map contains key that equals banana.
Maps are accessed the same way as normal nested values.
This means that you can use the $exists operator to check if the key exists.
db.collection.find( { "map.banana" : { $exists : true } } );