jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
286 stars 65 forks source link

Mongolite - Bulk update #158

Open dotdothu opened 5 years ago

dotdothu commented 5 years ago

Hi everyone, I use the package quite a lot but currently I have a headache with upserting multiple values. I would like to upsert in bulk, multiple documents with different values. In MongoDB the command looks like:


var bulk = db.dbName.initializeOrderedBulkOp();  
bulk.find({_id : 1}).upsert().update({$set: { test_1: "Y"}})
bulk.find({_id : 2}).upsert().update({$set: { test_1: "N"}})
bulk.execute();
db.dbName.find( { $or : [ { _id : 1 }, { _id : 2 } ]  } )  

I know that the update method does have a multiple parameter, however I could not figure out yet how to reproduce the above example with this package.

Is it possible at all currently?

Many thanks for the help, Andras

I've tried various things, non worked so far.


m$update(query=c('{"_id" : 1}','{"_id" : 2}')
         ,update = c('{"$set": { "test_1": "N"}}','{"$set": { "test_1": "Y"}}')
         ,multiple = TRUE,upsert=TRUE)

m$update(query='{ "$or" : [ { "_id" : 1 }, { "_id" : 2 } ]  } ',
         update = '{{"$set": { "test_1": "N"}}',
         multiple = TRUE,
         upsert = TRUE)
mmyrte commented 2 years ago

I presume the multiple toggle refers to db.collection.updateMany, which simply filter by one criterium - this should only work/make sense for the last query in your example, if I'm not mistaken. There's still no bulk operation implemented in mongolite, although the bundled mongoc source would have the methods in place. I imagine the mongo connection object could be used to represent the state of the bulk operation... but absent that, we'll just have to use some iterators.