Grails gorm和mongodb:嵌入式数组被保存为带数字键的地图

时间:2016-08-24 23:20:20

标签: mongodb grails gorm

我正在尝试将地图保存到文档中。某些地图值是地图,但其中一个特别是数组。此数组将保存为带有数字键而不是数组的Map。

class Post {

    ObjectId id
    String title
    String slug
    Date date
    String type
    Boolean published
    Map data

}

这是数据在数据库中的方式,Gorm从数据库返回的方式,以及我希望它在保存后保留的方式:

{
"_id" : ObjectId("577e9f1f78917ff50e1285e8"),
"slug" : "slug",
"title" : "title",
"published" : true,
"type" : "news",
"date" : ISODate("2016-07-07T07:00:00.000Z"),
"data" : {
    "content" : "post content here",
    "thumb" : "thumb.jpg",
    "featured_photo" : "featured.jpg",
    "tags" : "tags",
    "meta" : {},
    "video" : null,
    "interest" : "surf",
    "photos" : [ 
            {
                "source" : "image1.jpg",
                "caption" : "caption1",
                "thumb" : "thumb1.jpg"
            }, 
            {
                 "source" : "image2.jpg",
                 "caption" : "caption2",
                 "thumb" : "thumb2.jpg"
            }
        ]
    }
}

但最终看起来像这样:

{
"_id" : ObjectId("577e9f1f78917ff50e1285e8"),
"slug" : "slug",
"title" : "title",
"published" : true,
"type" : "news",
"date" : ISODate("2016-07-07T07:00:00.000Z"),
"data" : {
    "content" : "post content here",
    "thumb" : "thumb.jpg",
    "featured_photo" : "featured.jpg",
    "tags" : "tags",
    "meta" : {},
    "video" : null,
    "interest" : "surf",
    "photos" : { 
            "0" : {
                "source" : "image1.jpg",
                "caption" : "caption1",
                "thumb" : "thumb1.jpg"
            }, 
            "1" : {
                "source" : "image2.jpg",
                "caption" : "caption2",
                "thumb" : "thumb2.jpg"
            }
        }
    }
}

有没有办法可以指定“data.photos”应该存储为数组而不是地图?

1 个答案:

答案 0 :(得分:0)

我现在的解决方案是保留"数据"属性未映射,然后使用下标运算符来访问和保存属性,而不是让Gorm强制它进入Map。