This question already has answers here:
MongoDB: Unique index on array element's property
(6 answers)
Can you specify a key for $addToSet in Mongo?
(2 answers)
Closed 3 years ago.
I have the following structure in my Mongo document:
{
property1: 'value',
property2: 'value',
items: [
{
name: 'name1'
},
{
name: 'name2'
},
]
}
Is possible to define a local index over the items array?
In my case, item.name was a unique key but only in this document.
Other documents can have also a item with the same name that a item in other document (but the name in a document cannot be repeated).
Now I'm checking manually if the name is already in use before add/update elements in the document.items array, but if Mongo is capable to do it I will prefer...
EDIT: I have tried to create a index over items.name but if I create that index Mongo doesn't allows to insert/update a item if the name is already defined in other document.
Related
This question already has answers here:
MongoDB: How do I update a single subelement in an array, referenced by the index within the array?
(10 answers)
How do I update Array Elements matching criteria in a MongoDB document?
(2 answers)
Closed 4 years ago.
I have the following document:
{
_id: objectId("5aa7abb3c7dc133b0007e6e"),
name: "first course",
targets: Array
0: Object
id: "11111_rrrr",
value: "test"
1: Object
id: "22222_rrrr",
value: "hi"
}
so, I want to change the value of the array[i].
my solution is :
$update = array(
'targets.$[].value' => "New Value"
);
$result = Course::where("_id", $course_id)->update($update);
it changes all the value of the array, how can I solve my problem??
Try this
db.getCollection('TEST').update({"targets.id" : $course_id},{$set:{"targets.$.value" : "New Value"}})
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:
Can you specify a key for $addToSet in Mongo?
(2 answers)
Closed 6 years ago.
I'm using node.js and Mongo.db (I'm newly on Mongo). I have a document like this:
Tag : {
name: string,
videoIDs: array
}
The idea is, the server receives a JSON like
JSON: {
name: "sport",
videoId: "34f54e34c"
}
with this JSON, it has to find the tag with the same name and check if in the array it has the videoId, if not, insert it to the array.
How can I check the array and append data?
You can use $addToSet operator to check exist before append element into array.
db.tags.update(
{name: 'sport'},
{$addToSet: { videoIDs: "34f54e34c" } }
);
In this update statement example, mongoDB will find the TAG document which matches name == sport, and then check whether the videoIDs array contains 34f54e34c. If not, append it to the array.
Detail usage of $addToSet please read here.
I didn't test it, but you can try something like:
yourDb.find({ name: "sport",
videoIDs: { $in: ["34f54e34c"] } })
.update({$addToSet: { videoIDs: "34f54e34c" }});
If you need how: MongoDb in nodeJs implementation, you can start with:
http://jsfiddle.net/1ku0z1a1/
$addToSet operator check for the empty or existing array in document.
db.col_name.update({name :"name_for_match"},{$addToSet : { videoId : "video_id_value"}})
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 } } );
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