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']) }});
Related
This question already has answers here:
How to remove array element in mongodb?
(6 answers)
Closed 2 years ago.
I have been searching all day to figure out how to do a simple delete for one element inside a document that is inside a collection. I am fairly new to mongo scripts so bear with me.
I am currently using this script to see a certain document:
db.getCollection('roles').deleteOne({"_id": "e9b4afad-cb5c-4588-9560-161867faa5b7"})
This is what the document shows in Robo3T after the it is executed:
{
"_id" : "e9b4afad-cb5c-4588-9560-161867faa5b7",
"appIds" : [
"rpt",
"wc",
"bw"
],
}
I am simply wanting to remove "rpt" from appIds. I have tried this so far and it doesn't work:
db.getCollection('roles').find({"_id": "e9b4afad-cb5c-4588-9560-161867faa5b7"}).remove({"appIds": "rpt"})
Any documentation that'll point me in the right direction?
Thanks!
It is not an easy task but I believe $pull will do the work.
The $pull operator removes from an existing array (in a doc) all instances of a value or values that match a specified condition.
I would try,
db.roles.update(
{ },
{ $pull: { appIds: "rpt"}},
{ multi: true }
)
Let me know if it works...
If you want to update a specific id just add it in the first part {}, -> {_id: *****},
See the great documentation of MongoDB at https://docs.mongodb.com/manual/reference/operator/update/pull/
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}});
})
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
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"});
2 days old to Mongo, so bear with me.
I have a collection from which, I only want to retrieve specific values contingent to another key existing in the MongoDB environment.
Here is what I am doing:
db.results.find({'someKeyThatShouldExist':{$exists:true}}, {"parentKey.childKey.theKeyWoseValueIwant":1}
This yields data in the following format for me:
{ "_id" : ObjectId("532a2c2b6803fa486b8b456a"), "parentKey" : { "childKey" : { "theKeyWhoseValueIWant" : 102982577 }}}.....
Now, all I really want is the value 102982577, not everything else.
How can I do this ?
You can suppress the _id by adding _id:0 to the projection criteria.
db.results.find(
{"someKeyThatShouldExist":{$exists:true}},
{_id:0, "parentKey.childKey.theKeyWoseValueIwant":1}
)
To get just the value, you could do something like:
db.results.find(
{"someKeyThatShouldExist":{$exists:true}},
{_id:0, "parentKey.childKey.theKeyWoseValueIwant":1}
)[0].parentKey.childKey.theKeyWoseValueIwant