查询模型的嵌入列表

时间:2015-03-31 09:20:35

标签: mongodb grails

我有一个嵌入另一个模型的模型:

我的第一个模特看起来像这样

class Location {

    static mapWith = "mongo"

    String name
    String symbol

    List<LocationType> locationType

    static embedded = ['locationType']
}

第二个模型(这是在位置模型上嵌入LocationType的列表):

class LocationType {

    static mapWith = "mongo"

    List<LocaleEnum> locale
    Date dateCreated    
}

在我的mongodb数据库中,我有一个包含嵌入式LocationType模型列表的文档 该文件是:

{
    "_id" : NumberLong(11),
    "name" : "12",
    "locationType" : [ 
        {
            "dateCreated" : ISODate("2015-03-30T08:59:44.296Z"),
            "locale" : [ 
                "en", 
                "am"
            ]
        }, 
        {
            "dateCreated" : ISODate("2015-03-30T09:50:50.649Z"),
            "locale" : [ 
                "en"
            ]
        }, 
        {
            "dateCreated" : ISODate("2015-03-31T07:49:36.998Z"),
            "locale" : [ 
                "om"
            ]
        }
    ],
    "version" : NumberLong(2)
}

我想通过嵌入式模型的dateCreated从我的服务文档中查询此内容,并获取最近添加的locationType

1 个答案:

答案 0 :(得分:1)

只是做:

LocationType recentlyAddedLocType = locationObj.locationType.max { it.dateCreated }