Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird query behavior when using elemMatch #260

Open
Cipa opened this issue Sep 19, 2021 · 0 comments
Open

Weird query behavior when using elemMatch #260

Cipa opened this issue Sep 19, 2021 · 0 comments

Comments

@Cipa
Copy link

Cipa commented Sep 19, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant