Created
February 4, 2021 01:56
-
-
Save tangentus/99f53d79d4146099c65e1142fae0f1ca to your computer and use it in GitHub Desktop.
Mongo Ruby Driver bulk updates of embedded documents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Resources used to create this query | |
# https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-crud-operations/#updating | |
# https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/ | |
ProductCollection.with(client: :background) do |klass| | |
klass.collection.update_many( | |
{"product_references" => {"$exists" => true}}, # ensure that the `product_references` field exists | |
{ "$set" => { "product_references.$[elem].listed" => true } }, # says "update all documents matching the array filters below" | |
{ | |
# this `array_filters` allows you to set conditions on a field that is an array. The "elem" name is arbitrary and only needs to be the same everywhere it is used | |
array_filters: [ { "elem._id" => {"$in" => [BSON::ObjectId('5551207ed559303aee0007d0'), | |
BSON::ObjectId('53aca0f9e694aadaaf000350'), | |
BSON::ObjectId('53aca0e8d559309273000316'), | |
BSON::ObjectId('55512032bbddbd159c000761'), | |
BSON::ObjectId('5551206bd55930ceca0006bc'), | |
BSON::ObjectId('53aca10ad559304657000446'), | |
BSON::ObjectId('53aca104bbddbdc777000423')]} } ] | |
} | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment