# Pass MongoDB function to node-red-node-mongodb

**URL:** <https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101>\
**Category:** General\
**Created:** [13 January 2021 23:15 UTC](https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101 "2021-01-13T23:15:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adi-dev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/adi-dev/32/12511_2.png) [@adi-dev](https://discourse.nodered.org/u/adi-dev)\
**Post date:** [13 January 2021 23:15 UTC](https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101/1 "2021-01-13T23:15:45Z")

</div>

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

```auto
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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0ff34ef532e9b48cf648de2e995a7ce2f3a59543.png)

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

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [14 January 2021 07:38 UTC](https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101/2 "2021-01-14T07:38:39Z")

</div>

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

```javascript
functionGlobalContext: {
     
      // moment:require('moment'),
        ObjectId:require('mongodb').ObjectID
    },

```

1. Create a Function node with the following code :

```auto
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**

---

<div class="post-metadata">

**Author:** ![adi-dev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/adi-dev/32/12511_2.png) [@adi-dev](https://discourse.nodered.org/u/adi-dev)\
**Post date:** [14 January 2021 08:44 UTC](https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101/3 "2021-01-14T08:44:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [14 January 2021 09:23 UTC](https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101/4 "2021-01-14T09:23:35Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![adi-dev](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/adi-dev/32/12511_2.png) [@adi-dev](https://discourse.nodered.org/u/adi-dev)\
**Post date:** [14 January 2021 09:44 UTC](https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101/5 "2021-01-14T09:44:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 March 2021 09:44 UTC](https://discourse.nodered.org/t/pass-mongodb-function-to-node-red-node-mongodb/39101/6 "2021-03-15T09:44:59Z")

</div>

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