I have a document:
{ 'profile_set' :
[
{ 'name' : 'nick', 'options' : 0 },
{ 'name' : 'joe', 'options' : 2 },
{ 'name' : 'burt', 'options' : 1 }
]
}
and would like to add a new document to the profile_set set if the name doesn't already exist (regardless of the option).
So in this example if I tried to add:
{'name' : 'matt', 'options' : 0}
it should add it, but adding
{'name' : 'nick', 'options' : 2}
should do nothing because a document already exists with name nick even though the option is different.
Mongo seems to match against the whole element and I end up with to check if it's the same and I end up with
profile_set containing [{'name' : 'nick', 'options' : 0}, {'name' : 'nick', 'options' : 2}]
Is there a way to do this with $addToSet or do I have to push another command?
You can qualify your update with a query object that prevents the update if the name is already present in profile_set. In the shell:
db.coll.update(
{_id: id, 'profile_set.name': {$ne: 'nick'}},
{$push: {profile_set: {'name': 'nick', 'options': 2}}})
So this will only perform the $push for a doc with a matching _id and where there isn't a profile_set element where name is 'nick'.
As of MongoDB 4.2 there is a way to do this using aggregation expressions in update.
For your example case, you would do this:
newSubDocs = [ {'name' : 'matt', 'options' : 0}, {'name' : 'nick', 'options' : 2} ];
db.coll.update( { _id:1 },
[
{$set: { profile_set: {$concatArrays: [
"$profile_set",
{$filter: {
input:newSubDocs,
cond: {$not: {$in: [ "$$this.name", "$profile_set.name" ]}}
}}
]}}}
])
Related
I have a document:
{ 'profile_set' :
[
{ 'name' : 'nick', 'options' : 0 },
{ 'name' : 'joe', 'options' : 2 },
{ 'name' : 'burt', 'options' : 1 }
]
}
If I want to add new object to the profile_set only if the name of the object isn't already taken, regardless of the options, I can qualify my update with a query object that prevents the update if the name is already present in profile_set. In the shell:
db.coll.update(
{_id: id, 'profile_set.name': {$ne: 'nick'}},
{$push: {profile_set: {'name': 'nick', 'options': 2}}})
So this will only perform the $push for a doc with a matching _id and where there isn't a profile_set element where name is 'nick'.
Question But if I later need to change Nick's name (and maybe his options too...), that is change an existing array object, not add a new one. Is there a way to do that in one atomic update operation that still honor the unique constraint of name?
There are two conditions, I think:
var newName = "somename";
var oldName = "nick";
var newOption = 3;
// if not change the name
db.coll.update({
_id : id,
'profile_set.name' : oldName
}, {
$set : {
"profile_set.$.options" : newOption
}
});
// if change the name
db.coll.update({
_id : id,
$and : [ {
'profile_set.name' : {
$ne : newName
}
}, {
'profile_set.name' : oldName
} ]
}, {
$set : {
"profile_set.$.name" : newName,
"profile_set.$.options" : newOption
}
});
I have a document:
{ 'profile_set' :
[
{ 'name' : 'nick', 'options' : 0 },
{ 'name' : 'joe', 'options' : 2 },
{ 'name' : 'burt', 'options' : 1 }
]
}
and would like to add a new document to the profile_set set if the name doesn't already exist (regardless of the option).
So in this example if I tried to add:
{'name' : 'matt', 'options' : 0}
it should add it, but adding
{'name' : 'nick', 'options' : 2}
should do nothing because a document already exists with name nick even though the option is different.
Mongo seems to match against the whole element and I end up with to check if it's the same and I end up with
profile_set containing [{'name' : 'nick', 'options' : 0}, {'name' : 'nick', 'options' : 2}]
Is there a way to do this with $addToSet or do I have to push another command?
You can qualify your update with a query object that prevents the update if the name is already present in profile_set. In the shell:
db.coll.update(
{_id: id, 'profile_set.name': {$ne: 'nick'}},
{$push: {profile_set: {'name': 'nick', 'options': 2}}})
So this will only perform the $push for a doc with a matching _id and where there isn't a profile_set element where name is 'nick'.
As of MongoDB 4.2 there is a way to do this using aggregation expressions in update.
For your example case, you would do this:
newSubDocs = [ {'name' : 'matt', 'options' : 0}, {'name' : 'nick', 'options' : 2} ];
db.coll.update( { _id:1 },
[
{$set: { profile_set: {$concatArrays: [
"$profile_set",
{$filter: {
input:newSubDocs,
cond: {$not: {$in: [ "$$this.name", "$profile_set.name" ]}}
}}
]}}}
])
Let's assume the following schema:
{
'_id' : 'star_wars',
'count' : 1234,
'spellings' : [
{ spelling: 'Star wars', total: 10},
{ spelling: 'Star Wars', total : 15},
{ spelling: 'sTaR WaRs', total : 5} ]
}
I can update the count and one of the spellings by doing this:
db.movies.update(
{_id: "star_wars",
'spellings.spelling' : "Star Wars" },
{ $inc :
{ 'spellings.$.total' : 1,
'count' : 1 }}
)
But this form of update doesn't work with upsert. i.e., if I try to update (with upsert) with an _id that doesn't exist, or with a spelling that doesn't already exist, nothing happens.
Is there a solution that allows me to upsert when updating ($inc) a sub-document?
Thanks!
You could change your schema a little, though. If your documents looked like this:
{
'_id' : 'star_wars',
'count' : 1234,
'spellings' :
{
'Star wars': 10,
'Star Wars': 15,
'sTaR WaRs': 5
}
}
Your updates would become as simple as:
db.movies.update({_id:"star_wars"},{$inc:{"spellings.Star Wars":1}},true)
I have a document that's structured as follows:
{
'_id' => 'Star Wars',
'count' => 1234,
'spelling' => [ ( 'Star wars' => 10, 'Star Wars' => 15, 'sTaR WaRs' => 5) ]
}
I would like to get the top N documents (by descending count), but with only one one spelling per document (the one with the highest value). It there a way to do this with the aggregation framework?
I can easily get the top 10 results (using $sort and $limit). But how do I get only one spelling per each?
So for example, if I have the following three records:
{
'_id' => 'star_wars',
'count' => 1234,
'spelling' => [ ( 'Star wars' => 10, 'Star Wars' => 15, 'sTaR WaRs' => 5) ]
}
{
'_id' => 'willow',
'count' => 2211,
'spelling' => [ ( 'willow' => 300, 'Willow' => 550) ]
}
{
'_id' => 'indiana_jones',
'count' => 12,
'spelling' => [ ( 'indiana Jones' => 10, 'Indiana Jones' => 25, 'indiana jones' => 5) ]
}
And I ask for the top 2 results, I'll get:
{
'_id' => 'willow',
'count' => 2211,
'spelling' => 'Willow'
}
{
'_id' => 'star_wars',
'count' => 1234,
'spelling' => 'Star Wars'
}
(or something to this effect)
Thanks!
Your schema as designed would make using anything but a MapReduce difficult as you've used the keys of the object as values. So, I adjusted your schema to better match with MongoDB's capabilities (in JSON format as well for this example):
{
'_id' : 'star_wars',
'count' : 1234,
'spellings' : [
{ spelling: 'Star wars', total: 10},
{ spelling: 'Star Wars', total : 15},
{ spelling: 'sTaR WaRs', total : 5} ]
}
Note that it's now an array of objects with a specific key name, spelling, and a value for the total (I didn't know what that number actually represented, so I've called it total in my examples).
On to the aggregation:
db.so.aggregate([
{ $unwind: '$spellings' },
{ $project: {
'spelling' : '$spellings.spelling',
'total': '$spellings.total',
'count': '$count'
}
},
{ $sort : { total : -1 } },
{ $group : { _id : '$_id',
count: { $first: '$count' },
largest : { $first : '$total' },
spelling : { $first: '$spelling' }
}
}
])
Unwind all of the data so the aggregation pipeline can access the various values of the array
Flatten the data to include the key aspects needed by the pipeline. In this case, the specific spelling, the total, and the count.
Sort on the total, so that the last grouping can use $first
Then, group so that only the $first value for each _id is returned, and then also return the count which because of the way it was flattened for the pipeline, each temporary document will contain the count field.
Results:
[
{
"_id" : "star_wars",
"count" : 1234,
"largest" : 15,
"spelling" : "Star Wars"
},
{
"_id" : "indiana_jones",
"count" : 12,
"largest" : 25,
"spelling" : "Indiana Jones"
},
{
"_id" : "willow",
"count" : 2211,
"largest" : 550,
"spelling" : "Willow"
}
]
I have a DB like:
{
'a' : [
{ 'name' : 'john',
'phone' : 111111
},
{ 'name' : 'doe',
'phone' : 222222
},
],
'b' : [
{ 'name' : 'john',
'phone' : 111111
},
{ 'name' : 'doe',
'phone' : 222222
},
]
}
now I want to add a new field, "state : 1" to all the entries (like name, phone)
any suggestions on the update clause?
I tried using $set and $addToSet, but I'm not sure about the criteria too
thanks
You need to use the $ positional operator. See the documentation at http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator