查询模型的嵌入列表

Query an embedded list of a model

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

我的第一个模型是这样的

class Location {

    static mapWith = "mongo"

    String name
    String symbol

    List<LocationType> locationType

    static embedded = ['locationType']
}

second model(这是嵌入在 Location model 列表中的 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

就做:

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