[英]MongoDB Bson creating in C#
I am new to MongoDB, what I would like to do is Under property address I would like to save person's name and age, it might be one person per property or more than one, so I am trying with just name first but I am getting error. 我是MongoDB的新手,我想做的是在财产地址下,我想保存一个人的姓名和年龄,它可能是每个财产一个人或一个以上,所以我尝试仅使用名字,但是错误。
Json JSON
{
"property": "This is property address",
"address1": "address 1",
"address2": "address 2",
"city": "city name",
"people": [
"Person 1",
"Person 2",
"Person 3",
"Person 4"
]
}
C# code C#代码
List<string> names = new List<string>();
names.Add("person 1");
names.Add("person 2");
names.Add("person 3");
names.Add("person 4");
var document = new BsonDocument {
{"property", "10" },
{"address", "this is address 1" },
{"city", "city name" },
{"Family Members", new BsonArray {
new BsonDocument { {"name", names.ToBsonDocument() } }
} }
};
but I am getting bellow error 但我收到以下错误消息
An Array value cannot be written to the root level of a BSON document. 数组值不能写到BSON文档的根目录下。
look forward to you help. 期待您的帮助。
AFAIK, You con use BsonArray(IEnumerable<string> strings)
constructor like this: AFAIK,您可以像这样使用BsonArray(IEnumerable<string> strings)
构造函数:
var names = new[]
{
"Person 1",
"Person 2",
"Person 3",
"Person 4"
};
var document = new BsonDocument
{
{"property", "10"},
{"address", "this is address 1"},
{"city", "city name"},
{"Family Members", new BsonArray(names)}
};
That result is: 结果是:
{
{
"property" : "10",
"address" : "this is address 1",
"city" : "city name",
"Family Members" : ["Person 1", "Person 2", "Person 3", "Person 4"]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.