Mongo query on dynamic fields in embedded document [duplicate] - mongodb

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"});

Related

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

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}});
})

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

Mongodb find() query : return only unique values (no duplicates) [duplicate]

This question already has answers here:
Get distinct records values
(6 answers)
Closed 8 years ago.
I have a collection of documents :
{
"networkID": "myNetwork1",
"pointID": "point001",
"param": "param1"
}
{
"networkID": "myNetwork2",
"pointID": "point002",
"param": "param2"
}
{
"networkID": "myNetwork1",
"pointID": "point003",
"param": "param3"
}
...
pointIDs are unique but networkIDs are not.
Is it possible to query Mongodb in such a way that the result will be :
[myNetwork1,myNetwork2]
right now I only managed to return [myNetwork1,myNetwork2,myNetwork1]
I need a list of unique networkIDs to populate an autocomplete select2 component.
As I may have up to 50K documents I would prefer mongoDb to filter the results at the query level.
I think you can use db.collection.distinct(fields,query)
You will be able to get the distinct values in your case for NetworkID.
It should be something like this :
db.collection.distinct('NetworkID')

How to store reference to other collection in MongoDb [duplicate]

This question already has answers here:
MongoDb: Benefit of using ObjectID vs a string containing an Id?
(3 answers)
Closed 8 years ago.
I need to store reference to other collection and I can't decide if I should store it as a string, or as an ObjectId(). I see it is possible do it in both ways (in mongo shell):
As an ObjectId
db.books.findOne({_id:ObjectId("54bc1287c582714e9f062591")});
{
"_id" : ObjectId("54bc1287c582714e9f062591"),
"title" : "Book title",
"author_id" : ObjectId("54bc12da5f5e8854718b4568")
}
As a string
db.books.findOne({_id:ObjectId("54bc1287c582714e9f062591")});
{
"_id" : ObjectId("54bc1287c582714e9f062591"),
"title" : "Book title",
"author_id" : "54bc12da5f5e8854718b4568"
}
I will not be searching by author_id, so I don't need any index there. I'll take a book, and then will take an author by author_id. By the way, it is just an example with books
The major difference is that an ObjectId will take up 12 bytes space (http://docs.mongodb.org/manual/reference/object-id/) while the string representation takes 24 bytes. Thus, using ObjectId's will save you half the space.