Pass MongoDB function to node-red-node-mongodb

I just started to experiment with MongoDB as storage for my flows. I store some weather information using node-red-node-mongodb
I would like to execute (MongoDB):

db.getCollection('weather').remove({ _id: { $lt: ObjectId.fromDate(new Date(new Date().getTime() - 60 * 60*1000)) } })

Every 1 hr to remove old entries. Unfortunately, when I trigger inject function with text I have a reply:

MongoError: BSON field 'delete.deletes.q' is the wrong type 'string', expected type 'object'

When I set inject node to output type to JSON, NodeRED won't even accept this: Bad string
image

Any way to overcome this? Simple inserts are working alright, data is being populated in collection no problem.

Hi .. this needed a little bit of research.

You dont have a seperate Datetime field in your mongodb to search on and you want to search based on the _id ?

If you had a separate field it would be much easier since working with ObjectId is a hassle.

What i did to get it working and have access to a ObjectId in a Function node is :

  1. In the node-red settings.js file in the FunctionGlobalContext section add the following and restart NR
functionGlobalContext: {
     
      //  moment:require('moment'),
        ObjectId:require('mongodb').ObjectID
    },
  1. Create a Function node with the following code :
msg.collection = 'temperatures'   // use your collection
msg.operation = 'remove'

let ObjectId = global.get('ObjectId');   // define ObjectId from functionGlobalContext

let targetDate  = new Date(new Date().getTime() - 3 * 24 * 60 * 60*1000)   // 3 days ago

// convert targetDate to an ObjectId
var objectIdFromDate = function (date) {
	return Math.floor(date.getTime() / 1000).toString(16) + "0000000000000000";
};

// create payload for mongodb
msg.payload = { _id: { $lt: ObjectId(objectIdFromDate(targetDate)) } };


return msg;

** make a backup of your db if you have important data

I was afraid, you were going to say this. The solution will work, but it's a bit messy. I think, the better solution will be simple to create a timestamp numeric field. Thank you, @UnborN, for the reply.

yea ..
maybe with an update query you could add a field in for the old records but
if its a new database best to re-create it with the correct fields :wink:

This is a first time, I found NoSQL useful. This is like a temporary database, where I experiment before I go in production. But I'm going off the subject now.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.