0

So I have a collection called Transaction that is structured like the following

  {
        "_id": "1",
        "type": "purchase",
        "amount": 11.8,
        "relatedObjects": [
            {
                "eventId": "123131313131322"
            }
        ],
       "paymentMethod" : "method1"
    }

I want to query according to paymentMethod and eventId. So I was doing the following but it is not working.

db.Transaction.find({ "paymentMethod": "method1"});

and this is how I go all the transactions for "method1" but I am not sure on how should I query for eventId inside of related objects. I tried something like

db.Transaction.find({"paymentMethod": "method1", "relatedObjects[0].eventId" : "123131313131322" })

I didnt seem like it was going to work in my head but I have no clue on how to query an object inside an array.

Juan Diego
  • 1,396
  • 4
  • 19
  • 52

1 Answers1

0

Damn I had to read for third time the docs, sorry, It was something like

{
        "paymentMethod": "method1", 
        "relatedObjects" : {
            $elemMatch : {
                "eventId": "13213213212131321321"
            }
        }
}
Juan Diego
  • 1,396
  • 4
  • 19
  • 52