I have a function that takes in list of entry and save it to mongo using monger.
What is strange is that only the one record will be updated and the rest ignored unless I specify multi:true.
I don't understand why the multi flag is necessary for monger to persist all the updates to mongodb.
(defn update-entries
[entries]
(let [conn (mg/connect)
db (mg/get-db conn "database")]
(for [e entries] (mc/update db "posts" {"id" (:id e)} {$set {:data (:data e)}} {:multi true}))))
The multi flag is necessary for multi updates, since that's what mongo itself uses. Take a look at documentation for update. Granted, that's mongo shell, but most drivers try to follow when it comes to operation semantics.
Note that if
"id"is unique, then you're updating one record at a time so having:multiset totrueshouldn't matter.There is, however, another issue with your code.
You use a
forcomprehension, which in turn iterates a collection lazily, i.e. calls tomc/updatewon't be made until you force the realization of the collection returned byfor.Since
mc/updateis a call made for it's side-effects (update a record in the db), usingdoseqwould be more apropriate, unless you need the results.If that's the case, wrap
forindoallto force realization: