0

I've got a collection called payments with an example of its document shown below:

{
    "_id" : ObjectId("579b5ee817e3aaac2f0aebc1"),
    "updatedAt" : ISODate("2016-07-29T11:04:01.209-03:00"),
    "createdAt" : ISODate("2016-07-29T10:49:28.113-03:00"),
    "createdBy" : ObjectId("5763f56010cd7b03008147d4"),
    "contract" : ObjectId("578cb907f1575f0300d84d09"),
    "recurrence" : [ 
        {
            "when" : ISODate("2016-05-29T11:03:45.606-03:00"),
            "_id" : ObjectId("579b6241ea945e3631f64e2d"),
            "transaction" : {
                "createdAt" : ISODate("2016-05-29T11:03:45.608-03:00"),
                "tid" : "9999999999999999B01A",
                "status" : 4,
                "code" : "00",
                "message" : "Transação autorizada"
            },
            "status" : "PAGO"
        }, 
        {
            "when" : ISODate("2016-06-29T11:03:45.608-03:00"),
            "_id" : ObjectId("579b6241ea945e3631f64e2c"),
            "transaction" : {
                "createdAt" : ISODate("2016-06-29T11:03:45.608-03:00"),
                "tid" : "9999999999999999B01A",
                "status" : 4,
                "code" : "00",
                "message" : "Transação autorizada"
            },
            "status" : "PAGO"
        }, 
        {
            "when" : ISODate("2016-07-29T11:03:45.608-03:00"),
            "_id" : ObjectId("579b6241ea945e3631f64e2b"),
            "status" : "ERRO",
            "transaction" : {
                "code" : "56",
                "createdAt" : ISODate("2016-07-29T11:04:01.196-03:00"),
                "message" : "Autorização negada",
                "status" : 5,
                "tid" : "1006993069000730B88A"
            }
        }, 
        {
            "when" : ISODate("2016-07-30T11:03:45.608-03:00"),
            "_id" : ObjectId("579b6241ea945e3631f64e2a"),
            "status" : "PENDENTE"
        }, 
        {
            "when" : ISODate("2016-07-31T11:03:45.608-03:00"),
            "_id" : ObjectId("579b6241ea945e3631f64e29"),
            "status" : "PENDENTE"
        }, 
        {
            "when" : ISODate("2016-08-01T11:03:45.608-03:00"),
            "_id" : ObjectId("579b6241ea945e3631f64e28"),
            "status" : "PENDENTE"
        }
    ],
    "status" : "PAGO",
    "conditions" : {
        "originalValue" : 7406.64,
        "totalValue" : 7400,
        "upfrontValue" : 1500,
        "upfrontInstallments" : 3,
        "balanceInstallments" : 9
    },
    "__v" : 0,
    "transaction" : {
        "code" : "00",
        "createdAt" : ISODate("2016-07-29T10:49:46.610-03:00"),
        "message" : "Transação autorizada",
        "status" : 6,
        "tid" : "1006993069000730AF5A"
    }
}

If I run the query below, I get the desired document shown above:

db.payments.find({ "recurrence.transaction.tid": "1006993069000730B88A" })

However, if I run this other query, MongoDB returns my entire collection (presumably because it didn't match the subdocument's id):

db.payments.find({ "recurrence._id": ObjectId("579b6241ea945e3631f64e2b") })

Both queries should return the same result! I also checked some other questions including this one so unless I'm going crazy I'm doing the same thing. Not sure why the inconsistent results though.

Community
  • 1
  • 1
Andre Gallo
  • 2,261
  • 5
  • 23
  • 46
  • Are you sure that you don't have multiple documents with that ObjectId duplicated? – helmy Aug 03 '16 at 15:27
  • @helmy I was but now that you pointed this out I am reading this: http://stackoverflow.com/questions/4677237/possibility-of-duplicate-mongo-objectids-being-generated-in-two-different-colle and it does seem I am getting duplicate ids .. why ??? – Andre Gallo Aug 03 '16 at 15:41
  • Most likely an application bug would be my guess – helmy Aug 03 '16 at 15:47
  • @helmy it appears so ... Although the link in my comment above suggests that ObjectIds are unique across collections. Would that still be valid for embedded documents within collections? When I save a payment object, I simply do `payment.recurrence.push({ when: when, ... });` and thought this would automatically generate new ObjectIds which are unique – Andre Gallo Aug 03 '16 at 17:06
  • @helmy btw I'm using node.js and mongoose ... – Andre Gallo Aug 03 '16 at 17:08
  • It is not ObjectId but the _id field that is unique in a collection. – Andriy Simonov Aug 03 '16 at 18:04

1 Answers1

0

Tryout this:

db.payments.find({ recurrence : { $elemMatch: { "transaction.tid": "1006993069000730B88A"} } }).pretty()

Meena
  • 97
  • 2