【发布时间】:2016-08-04 13:45:40
【问题描述】:
我有一个名为 payments 的集合,其文档示例如下所示:
{
"_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,所以除非我快疯了,否则我会做同样的事情。不知道为什么结果不一致。
【问题讨论】:
-
您确定没有多个文档的 ObjectId 重复吗?
-
@helmy 我是,但现在你指出了这一点,我正在阅读:stackoverflow.com/questions/4677237/… 并且似乎我得到了重复的 id .. 为什么???
-
我猜很可能是应用程序错误
-
@helmy 看起来如此......尽管我上面评论中的链接表明 ObjectIds 在集合中是唯一的。这对集合中的嵌入文档仍然有效吗?当我保存一个支付对象时,我只是简单地做
payment.recurrence.push({ when: when, ... });并认为这会自动生成新的唯一的 ObjectIds -
@helmy btw 我正在使用 node.js 和 mongoose ...
标签: mongodb