# MongoDB filtering

**URL:** <https://discourse.nodered.org/t/mongodb-filtering/39828>\
**Category:** General\
**Created:** [26 January 2021 21:23 UTC](https://discourse.nodered.org/t/mongodb-filtering/39828 "2021-01-26T21:23:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![niclas.w82](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niclas.w82/32/31813_2.png) [@niclas.w82](https://discourse.nodered.org/u/niclas.w82)\
**Post date:** [26 January 2021 21:23 UTC](https://discourse.nodered.org/t/mongodb-filtering/39828/1 "2021-01-26T21:23:44Z")

</div>

Hi.

Trying to extract data from a mongoDB Database. Have succeeded to extrakt all the data from a database. But it is a lot of data. Can I filter somehow. From the attached picture I want only Objektid that contains "name" : "system.operator.login.ok",

 ![Skärmavbild 2021-01-26 kl. 22.03.15](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/599cf9779a3309447eaf3dad14248f5b7b0b2ab3.jpeg)

Have used mongodb2 and mongodb3

/Regards Niclas

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 January 2021 21:41 UTC](https://discourse.nodered.org/t/mongodb-filtering/39828/2 "2021-01-26T21:41:59Z")

</div>

I have not used MongoDB but I would have expected that you could request only those records when querying the database.

---

<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:** [26 January 2021 21:56 UTC](https://discourse.nodered.org/t/mongodb-filtering/39828/3 "2021-01-26T21:56:56Z")

</div>

Hi Nicas,

You can use Mongodb's [aggregation](https://docs.mongodb.com/manual/reference/operator/aggregation/match/#examples) to filter out the matching records.  
In a preceding Function node :

```javascript
msg.collection = "events"
msg.operation = "aggregate"

msg.payload = [

{ "$match": { "name": "system.operator.login.ok" } },

//{ "$sort": { dt: -1}},
//{"$limit": 2},
//{"$skip": 0}
];

return msg;

```

The above is untested since we dont have you're db but i hope it gives you the result you want  
ps. im using **node-red-node-mongodb**

---

<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:** [27 March 2021 21:57 UTC](https://discourse.nodered.org/t/mongodb-filtering/39828/4 "2021-03-27T21:57:46Z")

</div>

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