2016-08-03 68 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」}}})。漂亮的()