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 } } );
Related
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:
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.
This question already has answers here:
How to filter array in subdocument with MongoDB [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to find a specific object from array of objects in mongodb.
I am trying this
Company.findOne ({
"configuration.macAddress": "AB-90-dF-8d"
});
It returns me the exact company but it returns all the configuration array
I want only configuration with matching macAddress
Instead use aggregate(). $unwind the configuration array first, then you can $match the specific element only.
Company.aggregate([
{
"$unwind": "$configuration"
},
{
"$match":{
"configuration.macAddress": "AB-90-dF-8d"
}
}
]);
you can use $elemMatch to find a particular object in an array.
Company.find({ configuration: { $elemMatch: { macAddress: "AB-90-dF-8d"} } } );
can you show me your array of objects?
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')
This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 8 years ago.
Hey I'm trying to update all subdocuments contained within an array using the following code
setDuelToInactive: (duel) ->
Duels.update({_id: duel._id}, $set: {active: false})
userOneId = duel.userOneId
userTwoId = duel.userTwoId
Users.update({_id: userOneId}, { $set: { 'profile.character.souls.$.active': false} })
Users.update({_id: userTwoId}, { $set: { 'profile.character.souls.$.active': false} })
return
where the souls field in the Users collection is the array. I receive the following error message
MongoError: Cannot apply the positional operator without a corresponding query field containing an array.
You cannot use the positional operator $ to update al subdocuments inside an array. See the documentation: the operator matches the first element of the array that matches the query.
This also means that your query should use the array field (profile.character.souls) somehow. Yours do not, hence the error.
At this moment there is no shortcut for what you intend to do, you need to list all indexes manually. See this question: How to Update Multiple Array Elements in mongodb