Consider this data
{
"_id" : ...,
"array" : [
{ "name" : "value1","flag" : true } ,
{ "name" : "value2","flag" : false }
]
}
I would like to toggle the 2nd array element (from false to true)
I know I can update a specific element using the very useful $ positional operator like this:
db.myCollection.update(
{'array.name':'value2'},
{
$set: {
'array.$.flag':true
}
},false,true);
But is there a way to use the $ positional operator also for the value setting?
e.g. like this?
db.myCollection.update(
{'array.name':'value2'},
{
$set: {
'array.$.flag':'!array.$.flag' //<--
}
},false,true);
If you are using MongoDB 4.2, you can use aggregation operators in your update statement, like: . findOneAndUpdate({_id: day.id},[{$set:{present:{$eq:[false,"$present"]}}}]); That will set present to true if it is false, and to false if it is any other value.
To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
Learn how to update array fields in documents in MongoDB collections. You can use the updateOne() or updateMany() methods to add, update, or remove array elements based on the specified criteria. It is recommended to use the updateMany() method to update multiple arrays in a collection.
Boolean is a native field type in BSON (MongoDB's server-side storage format, aka "Binary JSON"). Booleans use less storage than an integer or string and avoid any unexpected side effects of comparison.
No, it's not possible at the moment. MongoDB doesn't allow expressions in updates that depend on fields in the document. You'll have to get and set in two separate operations.
However, there's a trick to make it in one operation (and therefore atomic). Instead of a boolean value, have an integer. Then even values will represent false
, odd ones - true
.
// get documents with false flag
db.collection.find({flag: {$mod: [2, 0]}})
// get documents with true flag
db.collection.find({flag: {$mod: [2, 1]}})
// toggle flag
db.collection.update(query, {$inc: {flag: 1}});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With