Closed p4ulcristian closed 5 years ago
Based on the source code for update
it looks like it should be called with:
db
"barbers"
{}
{"$set" ...}
:multi
is supposed to be specified in Monger, not as part of the document?Try this instead:
(mc/update db "barbers" {} {"$set" {"members.$[elem].text" "hellothere"}} {"arrayFilters" [{"elem.id" {"$eq" 1}}}} {:multi true})
i.e., move just :multi
out into the options argument map.
arrayFilters is a part of the options. Anyway I don't need the multi option, because I have distinct documents. So I updated my question without :multi
true option
Ah, I misread the nesting of the maps. Well, Monger doesn't pay attention to anything in the options map except multi
, upsert
, and write-concern
so if arrayFilters
truly is an option, Monger won't pass it to the MongoDB. See https://github.com/michaelklishin/monger/blob/6bf528ed5b8a21153e3df1aa0cd1d88e08f31e3a/src/clojure/monger/collection.clj#L265
Thank you for you help. Do you know any way of "hacking" this? Like, for example how could I use mongo shell from clojure.
Well, I found my "hack" using mg/command
.
(from-db-object (mg/command db (sorted-map :update "barbers" :updates [{:q {} :u {"$set" {"members.$[elem].text" "hellothere"}} :arrayFilters [{"elem.id" {"$eq" 1}}]}])) true)
This is my clojure function:
(mc/update db "barbers" {} {"$set" {"members.$[elem].text" "hellothere"}} {"arrayFilters" [{"elem.id" {"$eq" 1}}]})
The error I get is:
WriteConcernException Write failed with error code 2 and error message 'No array filter found for identifier 'elem' in path 'members.$[elem].text'' com.mongodb.operation.BaseWriteOperation.convertBulkWriteException (BaseWriteOperation.java:194)
The same in mongodb shell works:
db.barbers.update({},{$set :{"members.$[elem].text" :"hellothere"}},{"arrayFilters" : [{"elem.id": {$eq : 1}}]})
It returns
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })