# Node-red-contrib-mongodb4

**URL:** <https://discourse.nodered.org/t/node-red-contrib-mongodb4/71574>\
**Category:** General\
**Tags:** database\
**Created:** [1 December 2022 20:03 UTC](https://discourse.nodered.org/t/node-red-contrib-mongodb4/71574 "2022-12-01T20:03:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrSidX](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrsidx/32/18935_2.png) [@mrSidX](https://discourse.nodered.org/u/mrSidX)\
**Post date:** [1 December 2022 20:03 UTC](https://discourse.nodered.org/t/node-red-contrib-mongodb4/71574/1 "2022-12-01T20:03:54Z")

</div>

Moving to `node-red-contrib-mongodb4` and I'm having issues.  
I can perform a `find` operation, but I cannot for the life of me pass in any options like `$sort` or `$limit`

From a function node, I setup the collection and operations:

```auto
msg.collection = "foo";
msg.operation = "find";

```

I try two types of methods in attempt to implementing the options:

**method #1:**

```auto
 const options = {
     $sort : { "_id" : -1 },
     $limit : 1
}

msg.payload = [
    msg.searchQuery,
    options
];

```

and this function node passes the msg to a `node-red-contrib-mongodb4` node,  
which performs the find, but returns all documents.

I've also tried to do so by method of it's processor : `node-red-contrib-mongodb` (which worked well)  
by tacking on the `msg` object:

**method #2:**

```auto
msg.payload = msg.searchQuery

msg.sort = { "_id" : -1}
msg.limit = 1

return msg;

```

Still neither of these methods work for me and are still returning the full set of documents, and not limiting the return.

I just can't seem to get these `find` options to work.  
Not sure what I'm doing wrong here.

---

<div class="post-metadata">

**Author:** ![mrSidX](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrsidx/32/18935_2.png) [@mrSidX](https://discourse.nodered.org/u/mrSidX)\
**Post date:** [2 December 2022 17:14 UTC](https://discourse.nodered.org/t/node-red-contrib-mongodb4/71574/2 "2022-12-02T17:14:56Z")

</div>

I was able to do this with an aggregate operation, but the caveat was it needed double brackets??

```auto
msg.collection = global.get("db")["submissions"];
msg.operation = "aggregate"

msg.payload = // Pipeline
    [[
        // Stage 1
        {
            $match: {
                // conditional query 
                "season_code": "CODE2021"
            }
        },
        // Stage 2
        {$limit : 1}

    ]]

```

This code example produced a working result when passed to the node.

---

<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:** [16 December 2022 17:15 UTC](https://discourse.nodered.org/t/node-red-contrib-mongodb4/71574/3 "2022-12-16T17:15:55Z")

</div>

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