2016-08-03 54 views
0

嵌入文档我有一个集合称为支付与下表所示的文件的例子:不一致的查询结果与MongoDB的

{ 
    "_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" 
    } 
} 

如果我运行下面的查询,我得到显示所需的文档以上:

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

但是,如果我运行这个其他查询,MongoDB的返回我的整个集合(大概是因为它不匹配的子文档的id):

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

这两个查询都应返回相同的结果!我还检查了其他一些问题,包括这个one,所以除非我发疯,否则我也在做同样的事情。不知道为什么不一致的结果。

+0

你确定你没有与该ObjectId重复的多个文档? – helmy

+0

@helmy我是但现在你指出了这一点,我正在阅读:http://stackoverflow.com/questions/4677237/possibility-of-duplicate-mongo-objectids-being-generated-in-two-different-colle它似乎我得到重复ID ..为什么? –

+0

最有可能的应用程序错误将是我的猜测 – helmy

回答

0

试用此:

db.payments.find({复发:{$ elemMatch:{ “transaction.tid”: “1006993069000730B88A”}}})。漂亮的()