Leonardo Lima
Leonardo Lima

Reputation: 140

Query subdocuments without knowing the keys

I have a collection like this:

{
"_id" : ObjectId("5a7c49b02d2bbb28a4b2e6a2"),
    "phone" : "Pinheiro",
    "email" : "Pinheiro",
    "variableParameters" : {
        "loremIpsum" : "Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it?",
        "uf" : "Rio de Janeiro",
        "city" : "Rio de Janeiro",
        "end" : "RUA JARDIM BOTÂNICO 1060",
        "tel" : "5521999999999",
        "eml" : "[email protected]",
        "nome" : "Usuario de Teste"
    }
}

And i want to query the "variableParameters" object, but like the name said, this properties are variable. So in some cases it will have "uf", but in other cases won't.

I'm actually doing a query that only matches the constant field from a mongoose schema:

{ 'phone': { $regex: filter, $options: 'i' } }

Is there any way that I can query "variableParameters" without knowing his child properties?

Upvotes: 0

Views: 99

Answers (3)

Leonardo Lima
Leonardo Lima

Reputation: 140

[SOLVED]

Thanks @Clement Amarnath for the help.

The solution is something like this:

_Events.find({ $text: { $search: 'searchText' } }, (err, events) => {
    if (err) return Exceptions.HandleApiException(err, res);
    res.send(events);
});

The $text parameter can have this properties:

{
  $text: {
      $search: <string>,
      $language: <string>,
      $caseSensitive: <boolean>,
      $diacriticSensitive: <boolean>
  }
}

$search A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase. See Behavior for more information on the field.

$language Optional. The language that determines the list of stop words for the search and the rules for the stemmer and tokenizer. If not specified, the search uses the default language of the index. For supported languages, see Text Search Languages.

If you specify a language value of "none", then the text search uses simple tokenization with no list of stop words and no stemming.

$caseSensitive Optional. A boolean flag to enable or disable case sensitive search. Defaults to false; i.e. the search defers to the case insensitivity of the text index.

$diacriticSensitive Optional. A boolean flag to enable or disable diacritic sensitive search against version 3 text indexes. Defaults to false; i.e. the search defers to the diacritic insensitivity of the text index.

For more information, see MongoDB Documentation.

Upvotes: 1

Kroney
Kroney

Reputation: 73

You can use $where to build your own matching function. An example for a full-text match would be:

db.col.find({$where: function(){
  return Object.values(this.variableParameters).includes("Rio de Janeiro")
}})

Upvotes: 0

Clement Amarnath
Clement Amarnath

Reputation: 5466

If you are unsure about the keys(since they are variable), then try using $text search

To use text search we need to index the variableParameters.

Case sensitive text search can also be performed but it comes with the impact on performance.

Please read https://docs.mongodb.com/manual/reference/operator/query/text/ for more information on text search

Upvotes: 1

Related Questions