Node-red-node-mongodb and wildcards

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?

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

your syntax doesnt resemble with the syntax given in the Mongodb documentation

{
  $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)

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

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.

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. ;*)

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!

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