Skip to content

Weird query behavior when using elemMatch #260

@Cipa

Description

@Cipa

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions