MongoDB-如何根据键名查询文档?

时间:2018-12-18 01:08:38

标签: mongodb

我的收藏集具有与此相似的文档:

{
"_id" : "123456789",
"createdDateTime" : ISODate("2018-10-10T14:48:38.314Z"),
"fields" : {
    "session" : null,
    "information" : [ 
        {
            "userID" : {
                "changedDate" : NumberLong(1539129767860),
                "before" : "12",
                "after" : "13",
                "jaro" : 0.66
            },
            "distance" : 2838,
            "Account" : "55555",
            "accountDate" : {
                "changedDate" : NumberLong(1539129767860),
                "before" : NumberLong(1293858420000),
                "after" : NumberLong(1293858420000),
                "jaro" : 1.0
            }
        }, 
        {
            "distance" : 2838,
            "Address" : {
                "changedDate" : NumberLong(1539134488307),
                "before" : "NY",
                "after" : "NJ",
                "jaro" : 0.0
            },
            "Account" : "66666",
            "accountDate" : {
                "changedDate" : NumberLong(1539134488307),
                "before" : NumberLong(1293858420000),
                "after" : NumberLong(1293858420000),
                "jaro" : 1.0
            }
        }
    ]
}
}

如您所见,信息键具有一组嵌套对象。第一个对象具有 UserID 键,第二个对象具有 Address 键;除此之外,两个对象都有距离,一个account和accountDate键。

我想查询存在地址更改的元素,但只想提取包含 Address 键的元素,在这种情况下,我想要这样:

{
"_id" : "123456789",
"createdDateTime" : ISODate("2018-10-10T14:48:38.314Z"),
"fields" : {
    "session" : null,
    "information" : [ 
        {
            "distance" : 2838,
            "Address" : {
                "changedDate" : NumberLong(1539134488307),
                "before" : "LYNN",
                "after" : "GEORGETOWN",
                "jaro" : 0.0
            },
            "Account" : "66666",
            "accountDate" : {
                "changedDate" : NumberLong(1539134488307),
                "before" : NumberLong(1293858420000),
                "after" : NumberLong(1293858420000),
                "jaro" : 1.0
            }
        }
    ]
}
}

我该如何查询?

我的mongodb是3.2

谢谢!

0 个答案:

没有答案