jeroen / mongolite

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

How to run a $convert update query with $update or $run command ? #235

Open Wesseldr opened 2 years ago

Wesseldr commented 2 years ago

Hi,

I'm trying to execute this query:

 db.rawclients.update(
    { _clientContract: { $type: 2 } },
    [{ $set: { _clientContract: { $convert: { input: "$_clientContract", to: 7 } } } }],
    { multi: true }
);

I'm pretty stuck... why something simple like:

query <- 'db.rawclients.update( 
          { _clientContract : { $type: 2 } }, 
          [{ $set: { _clientContract: { $convert: { input: "$_clientContract", to: 7 } } } }], 
          { multi: true } )'

Using build in $update:

rawclients$update(
    '{ _clientContract : { $type: 2 } }',
   '[{ $set: { _clientContract: { $convert: { input: "$_clientContract", to: 7 } } } }]',
   multiple = TRUE
)

Using run:

rawclients$run(query)

Is not working, what am I'm missing here ... al also tried the json command notation like

$run(command = "{\"ping\": 1}", simplify = TRUE) # that works.... instead adding the whole query inside like
$run(command = glue("{\"{query}\"): 1}", simplify = TRUE) # just blows up with a JSON error.

Putting " around every key/value also still fails.

Hope someone has a hint how to run this $type update query... I'm inserting a large table in tibble format into mongodb that works great :), and after the insert I'm "restoring" the ObjectId's with this query :) During the insert it creates string fields for the foreigner keys I'm using in my document.. no problem expected behaviour but however hope to clean it up afterwards like this :)

JWR