问题
I have such a schema:
doc:
{
//Some fields
visits:
[
{
userID: Int32
time: Int64
}
]
}
I want to first check if a specific userID
exists, if not, push a document with that userID
and system time
, else just update time
value. I know neither $push
nor $addToSet
are not able to do that. Also using $
with upsert:true
doesn't work, because of official documentation advice which says DB will use $
as field name instead of operator when trying to upsert.
Please guide me about this. Thanks
回答1:
You can use $addToSet
to add an item to the array and $set
to update an existing item in this array.
The following will add a new item to the array if the userID
is not found in the array :
db.doc.update({
visits: {
"$not": {
"$elemMatch": {
"userID": 4
}
}
}
}, {
$addToSet: {
visits: {
"userID": 4,
"time": 1482607614
}
}
}, { multi: true });
The following will update the subdocument array item if it matches the userId
:
db.doc.update({ "visits.userID": 2 }, {
$set: {
"visits.$.time": 1482607614
}
}, { multi: true });
来源:https://stackoverflow.com/questions/41316056/mongodb-update-array-element-document-with-a-key-if-exists-else-push