# Node-red-node-mongodb and wildcards

**URL:** <https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876>\
**Category:** General\
**Created:** [20 December 2023 16:37 UTC](https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876 "2023-12-20T16:37:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisofCCL](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisofccl/32/42442_2.png) [@chrisofCCL](https://discourse.nodered.org/u/chrisofCCL)\
**Post date:** [20 December 2023 16:37 UTC](https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876/1 "2023-12-20T16:37:58Z")

</div>

First time on this forum for ages and i have a tricky MongoDB query.

I'm using the node-red-node-mongodb nodes and I want to perform a query using wildcards. From the documentation, I have come up with the following as the msg.payload going into a 'mongodb in' node set to Find.

{"queriedProperty":{"wildcard:{"query":"\<string with a ? or \* \>","allowAnalyzedField":true} }

But it doesn't work, and returns an 'empty' response. Any ideas anyone?

---

<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:** [20 December 2023 17:35 UTC](https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876/2 "2023-12-20T17:35:35Z")

</div>

Hello Chris,

are you using MongoDb Atlas or the local mongodb ?  
from what i read the $search wildcard feature is only available for Atlas

i think the $search command is also available with **Aggregation** and not with **Find**

> [@chrisofCCL](#):
>
> {"queriedProperty":{"wildcard:{"query":"\<string with a ? or \* \>","allowAnalyzedField":true} }

your syntax doesnt resemble with the syntax given in the [Mongodb documentation](https://www.mongodb.com/docs/atlas/atlas-search/wildcard/#syntax)

```auto
{
  $search: {
    "index": <index name>, // optional, defaults to "default"
    "wildcard": {
      "query": "<search-string>",
      "path": "<field-to-search>",
      "allowAnalyzedField": <boolean>,
      "score": <options>
    }
  }
}

```

From what i gather your are searching for documents that have either a `?` or a `*` in their values ?  
What is the "path" field of your document you want to do a search on ? Is it called "queriedProperty" ?

maybe something like this ? (cannot test as i have the local mongodb)

```auto
msg.collection = "test"
msg.operation = "aggregate"

msg.payload = [
    {
        "$search": {
            "wildcard": {
                "path": "queriedProperty",
                "query": ["*\\?*", "*\\**"] // can be a string or array of strings
            }
        }
    }];

return msg;

```

`*` used to match any character before and after  
`\\` is used to escape a character

ps. you may also want to look at [$regex](https://www.mongodb.com/docs/manual/reference/operator/query/regex/)

---

<div class="post-metadata">

**Author:** ![chrisofCCL](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisofccl/32/42442_2.png) [@chrisofCCL](https://discourse.nodered.org/u/chrisofCCL)\
**Post date:** [20 December 2023 17:42 UTC](https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876/3 "2023-12-20T17:42:43Z")

</div>

Thanks, but I think you've hit the nail on the head. I'm using local, so I'll have to think of a better way to do what I want to do.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [20 December 2023 19:22 UTC](https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876/4 "2023-12-20T19:22:47Z")

</div>

> [@chrisofCCL](#):
>
> {"queriedProperty":{"wildcard:{"query":"\<string with a ? or \* \>","allowAnalyzedField":true} }

Could the problem be that your JSON string is not valid? There are 3 open braces and only 2 close braces...

Does the node specifically state that the input `msg.payload` needs to be a string? If not, try passing that information as a Javascript object. At least that way you can't have invalid syntax, only bad data. ;\*)

---

<div class="post-metadata">

**Author:** ![chrisofCCL](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisofccl/32/42442_2.png) [@chrisofCCL](https://discourse.nodered.org/u/chrisofCCL)\
**Post date:** [20 December 2023 19:57 UTC](https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876/5 "2023-12-20T19:57:37Z")

</div>

Thanks Shrickus, but the {} issue was just finger trouble in my post. Pretty sure I have just been using the wrong flavour of Mongo.

I've worked out a different way of doing what I need to do now, so no more replies needed thanks!

---

<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:** [18 February 2024 19:57 UTC](https://discourse.nodered.org/t/node-red-node-mongodb-and-wildcards/83876/6 "2024-02-18T19:57:47Z")

</div>

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