MongoDB上嵌入文档的查询结果不一致

时间:2016-08-03 15:14:34

标签: 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,所以除非我发疯,否则我会做同样的事情。不知道为什么会出现不一致的结果。

1 个答案:

答案 0 :(得分:0)

试试这个:

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