Spring Data Mongo-确定基于多个字段的嵌入式文档?

时间:2019-04-11 08:22:33

标签: mongodb spring-data-mongodb

我从这里延伸相同的问题:find distinct embedded documents from the document using Spring Data Mongo?。我看到了问题,或者我需要一些规定来做出决定。

我正在关注两个文档。

/* 1 */
{
    "_id" : ObjectId("5caef73953a9dc193c9aa8e1"),
    "firstName" : "Ravi",
    "emailId" : "ravi@gmail.com",
    "hobbies" : [ 
        {
            "interest" : "Indoor",
            "sports" : "Chess",
            "status" : "Active"
        }, 
        {
            "interest" : "Loveoor",
            "sports" : "Table Tennis"
        }, 
        {
            "interest" : "Indoor",
            "sports" : "Chess"
        }
    ],
    "_class" : "com.example.Person"
}

/* 2 */
{
    "_id" : ObjectId("5caef73953a9dc193c9aa8e2"),
    "firstName" : "John",
    "emailId" : "john.kerr@gmail.com",
    "hobbies" : [ 
        {
            "interest" : "Indoor",
            "sports" : "Chess",
            "status" : "Active"
        }, 
        {
            "interest" : "Loveoor",
            "sports" : "Table Tennis"
        }
    ],
    "_class" : "com.example.Person"
}

如果我对不同的db.getCollection('person').distinct("hobbies")进行查询,则会得到以下结果。

[
    {
        "interest" : "Indoor",
        "sports" : "Chess"
    },
    {
        "interest" : "Indoor",
        "sports" : "Chess",
        "status" : "Active"
    },
    {
        "interest" : "Loveoor",
        "sports" : "Table Tennis"
    }
]

如果您仔细观察前两个嵌入的子文档,则它们相同,仅Active字段不同。我想根据interestsports字段做出决定。

如何使用Spring Data Mongo, MongoTemplate来做到这一点?

List<Object> object = mongoTemplate.query(Person.class).distinct("hobbies").all();

0 个答案:

没有答案