ozomer / node-red-contrib-mongodb2

MongoDB driver node for Node-RED
Apache License 2.0
15 stars 19 forks source link

deleting collection by ObjectID #26

Closed RafOXxxx closed 6 years ago

RafOXxxx commented 6 years ago

I am not able to delete documents by "_id" using the deleteOne() method. I presume it's because I am passing the id as a string and not as an ObjectId object. But saying that, how do I turn a string into an ObjectId object?

ozomer commented 6 years ago

Install mongodb and use mongodb.ObjectId(). Maybe { "$id": "..." } will also work but I'm not sure.

RafOXxxx commented 6 years ago

Thanks for the response, the { "$id": "..." } approach did not work for me. I installed node-red-contrib-objectid, that made it work.

Are there newer example-flows for node-red-contrib-mongodb2? Because I also have trouble sorting my results. In examples I have seen so far people just do:

msg.sort = { time: -1 }

but that does not seem to work anymore.

ozomer commented 6 years ago

Great, I didn't know node-red-contrib-objectid exists. I just install mongodb and use const { ObjectId } = require('mongodb');. node-red-contrib-mongodb2 only looks at the msg.payload param - which should be an array of arguments that are passed to the mongodb operations. For example, the find operation can accept two parameters: query and options. In options you can use "sort", so you may want to create a msg with a payload that looks like: [{ somequeryfield: somevalue }, { sort: { time: -1 }}].

RafOXxxx commented 6 years ago

Thank god! I have been googling for this information for a couple of days now and somehow could only find very misleading stuff. I can't thank you enough ozomer! 😃 👍

ozomer commented 6 years ago

You're welcome @RafOXxxx !