将图片数组添加到Mongodb中

时间:2013-01-14 07:27:47

标签: c# mongodb

下面我有一个Mongodb数据库结构,我想在结构中添加list of picture

{
    "_id": "50d3dbce1292dd2e98af1dd1",
    "Name": "Bubba",
    "Address": "1111",
    "Loc": {
        "Lon": 11.0000,
        "Lat": 3.113005
    },
    "Pic" : [{"Name": "test1.jpg", "Size":"1000"}, {"Name": "test2.jpg", "Size":"2000"}],
    "LastModified": {
        "$date": "2012-12-21T03:47:26.535Z"
    }
}

我的模特:

public class Test
    {
        [BsonId]
        public string Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public Location Loc { get; set; }
        public Picure Pic {get; set; }
        public DateTime LastModified { get; set; }
    }

    public class Location
    {
        public double Lon { get; set; }
        public double Lat {get; set;}
    }

    public class Picture
    {
        public string Name{ get; set;}
        public int Size {get; set;}
    }

我的图片模型阵列的设计是否正确?

1 个答案:

答案 0 :(得分:1)

在您的文档Pic中是一个数组。因此,您应该将Pic属性声明为List<Picture>

public List<Picture> Pic {get; set; }


你的json也有错误:

{"Name", "test1.jpg", "Size":"1000"}

但应该是

{"Name": "test1.jpg", "Size":"1000"}