jeroen / mongolite

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

wrap `findAndModify` #177

Open phaverty opened 5 years ago

phaverty commented 5 years ago

It would be awesome to have access to the mongo function findAndModify. I've been looking into implementing this trick for an auto-incrementing field. On a related note, would also be great to have documentation on calling a server-side javascript function from mongolite. I'm not sure it is possible at the moment as the function call ends up as a character string in the BSON object. but, maybe I'm missing something. I've tried running findAndModify with the simple command interface, without success. Might we have some more documentation on using that with more complex functions? Any help in this area would be much appreciated!

ceparman commented 4 years ago

UPDATE:

I was able to get this to work with $run. This example run against collection metadb. The document has name and count fields. This command returns the updated object.

db$run( '{"findAndModify":"metadb","query": {"name" : "Tube"},"update":{"$inc":{"count":1}}, "new": "true"}' )


I am trying this trick and running into the same issue. findAndModify returns the updated document which I believe update does not. I am not sure how to format something like

db.people.findAndModify({ query: { name: "Andy" }, update: { $inc: { score: 1 } } })

to be used in the run method. Any help would be appreciated.