ForerunnerDB icon indicating copy to clipboard operation
ForerunnerDB copied to clipboard

Weird query behavior when using elemMatch

Open Cipa opened this issue 3 years ago • 0 comments

Hi,

When using

[
  {
    "_id": 1,
    "name": "Player 1",
    "might": 2,
    "endurance": 7,
    "inventory": [
      {
        "name": "Battery",
        "desc": "A battery",
        "qty": 2
      },
      {
        "name": "Rations",
        "desc": "Food",
        "qty": 2
      }
    ]
  }
]

this query works in the playground you provide

{
              "$and": [
                {
                  "inventory": {
                    "$elemMatch": {
                      "name": "Battery",
                      "qty": {
                        "$gte": 2
                      }
                    }
                  }
                },
                {
                  "inventory": {
                    "$elemMatch": {
                      "name": "Rations",
                      "qty": {
                        "$gte": 1
                      }
                    }
                  }
                }
              ]
            }

but it doesn't work in my local setup using Vue, but this one works

{
          $and: [
            {
              inventory: {
                $and: [
                  { name: "Battery" },
                  {
                    qty: {
                      $gte: 2,
                    },
                  },
                ],
              },
            },
            {
              inventory: {
                $and: [
                  { name: "Rations" },
                  {
                    qty: {
                      $gte: 1,
                    },
                  },
                ],
              },
            },
          ],
        }

Anything that I am doing wrong?

const collection = db.collection(aw.who);
const result = collection.find(test); //test is the queries above

Thank you

Cipa avatar Sep 19 '21 01:09 Cipa