问题:我有一个包含两个数组的Mongo文档。其中一个数组很大,带有子文档。这个序列化没有问题。另一个是这种类型的简单数组:
staffgroups {“Tech”,“Sales”}
这个不会序列化。我得到错误说它是BsonArray。我能够最接近序列化产生一个字符串。我需要一个JSON对象。
代码时间:
public class specialtyGroup
{
public ObjectId _id { get; set; }
public string name { get; set; }
public string location { get; set; }
public coachConfig config { get; set; }
public schedules[] coaches { get; set; }
public BsonArray staffgroups { get; set; }
}
和webservice:
public void GetGroups()
{
var client = new MongoClient();
var db = client.GetDatabase("MongoTul");
var coll = db.GetCollection<specialtyGroup>("specialtyGroups");
string cname = HttpContext.Current.Request.Params["loc"];
var creatures = coll.Find(b => b.location == cname)
.ToListAsync()
.Result;
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(creatures));
}
我尝试过使用聚合和投影。我已经尝试为staffgroups创建一个额外的类(适用于我的复杂数组)。还有其他一些事情。一切都不好。
最常见的错误如下所示:无法将类型为“MongoDB.Bson.BsonString”的对象强制转换为“MongoDB.Bson.BsonBoolean”。
在发布之前我花了好几个小时才发布,然后发布后我在30分钟内搞清楚了。抱歉。
答案是员工组应该是“public string [] staffgroups {get; set;}
因此,如果像我这样的任何其他rubes都有这个问题,那就是答案。