0

For Mongo, for an array I am able to use in peace:

db.fighters.find({"close_attacks": {$size: 3}},
                 {"_id": 0, "biography": 0})
           .pretty()

and same for >, , < and , for example:

  • 2>
  • 2≥
  • <5
  • ≤5

Therefore the query would be as template for example:

db.fighters.find({$and: [{"close_attacks": {$type: "array"}},
                         {$expr: {$X: [{$size: "$close_attacks"}, 3]}}]})

Where $X can be any of $lt, $lte, $gt and $gte

About the pattern shown above see:

I want to know if is possible and how declare one query but working with a range about the size, such as:

  • 2> and <5 (exclusive)
  • 2≥ and ≤5 (inclusive)
Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
  • You can build the query filter based upon _some input_ - and use that filter with the query. For example, like in this post: https://stackoverflow.com/questions/69367673/mongoose-find-function-is-there-a-value-for-in-which-would-be-true-no-matter-w/69370680#69370680 . So does your query take any inputs to determine the range, for example? – prasad_ Dec 29 '21 at 05:51
  • You can use another `$and` insided that expr to pass both conditions, exactly like you do it at the first level. – Rubén Vega Dec 29 '21 at 08:39
  • You have **one** query! What is your question? – Wernfried Domscheit Dec 29 '21 at 10:47
  • @WernfriedDomscheit I have currently one query without a range. The question is have one query with a range or between of dates. – Manuel Jordan Dec 29 '21 at 19:56
  • @prasad_ I saw your link, the query requested should be easy/simple. – Manuel Jordan Dec 29 '21 at 19:57
  • 2
    Try this with an `$and` operator: `$expr: { $and: [ { $gt: [ { $size: "$close_attacks" }, 2 ] }, { $lt: [ { $size: "$close_attacks" }, 5 ] } ] }` – prasad_ Dec 30 '21 at 03:19

0 Answers0