无法检索 mongodb c# 中的子文档项
Unable to retrieve subdocument items in mongodb c#
模型结构:
//Main doc model
Public class MainDocumentModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name{ get; set; }
public List<SubDocuementModel> SubDocsList { get; set; }
}
//sub doc model
Public class SubDocumentModel
{
public string Id { get; set; }
[BsonIgnoreIfNull]
public string MainDocId { get; set; }
public string Name{ get; set; }
[BsonIgnoreIfNull]
public List<SubDocuementModel> SubDocsList { get; set; }
.
.
.
}
当我在数据库中插入MainDocuement
时,它的结构如下:
{
"_id" : ObjectId("54b9d732bb63bc10ec9bad6f"),
"Name" : "Name-1",
"SubDocsList : [{
"Id" : "54b9e76dbb63bc1890e8761b",
"Name" : "Sub-Name-1"
},....]
}
现在我想检索 main doc Id == 54b9d732bb63bc10ec9bad6f
:
的子文档
private void getSubDoc(SubDocumentModel oModel)
{
_MainDoc = _db.GetCollection<MainDocumentModel>("MainDoc");
_query = Query<MainDocumentModel>.Where(e => e.Id == oModel.MainDocId);
//Here I get the exception
//Exception: An error occurred while deserializing the SubDocsList property
of class Data.MainDocumentModel: Element 'Id' does not match any field or
property of class Data.SubDocumentModel.
private MainDocumentModel _mainDocModel = _MainDoc.FindOneAs<MainDocumentModel>(_query);
oModel.SubDocsList =
_mainDocModel .SubDocsList .FindAll(
x => x.Name.ToLower().Contains("NameFilter"));
//Pagination
oModel.SubDocsList =
oModel.SubDocsList .Take(oModel.ItemsPerPage)
.Skip(oModel.ItemsPerPage*(oModel.PageNo - 1))
.ToList();
}
为什么我会遇到 反序列化 异常。我做错了什么?
在这里,我将分页逻辑应用于检索到的 c# 列表。我如何在 mongo 查询中应用 pagination 本身?
为主文档应用分页非常容易:
mainDocCursor = MainDoc .Find(_query).SetSortOrder(SortBy.Ascending("Name")).SetLimit(oModel.ItemsPerPage).SetSkip(oModel.ItemsPerPage * (oModel.PageNo - 1));
如何实现子文档相同的分页?
我通过以下小改动解决了反序列化异常:
在子文档模型中:
Public class SubDocumentModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
.
.
.
}
当我插入子文档时,我将 Id 保存为对象 ID 而不是字符串(之前我将其保存为导致问题的字符串):
public void addSubDocument(SubDocumentModeloModel)
{
_MainDoc = _db.GetCollection<MainDocumentModel>("MainDoc");
BsonDocument subDocument = new BsonDocument
{
{"_id", ObjectId.GenerateNewId()},//Previously it was {"Id", ObjectId.GenerateNewId().ToString()}
{"Type", oModel.Name}
};
_query = Query<MainDocumentModel>.Where(e => e.Id == oModel.MainDocId);
_updateBuilder = Update.Push("SubDocsList", subDocument);
_MainDoc.Update(_query, _updateBuilder);
}
但是我还没有找到在 mongodb-c#
中对子文档进行分页的方法
模型结构:
//Main doc model
Public class MainDocumentModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name{ get; set; }
public List<SubDocuementModel> SubDocsList { get; set; }
}
//sub doc model
Public class SubDocumentModel
{
public string Id { get; set; }
[BsonIgnoreIfNull]
public string MainDocId { get; set; }
public string Name{ get; set; }
[BsonIgnoreIfNull]
public List<SubDocuementModel> SubDocsList { get; set; }
.
.
.
}
当我在数据库中插入MainDocuement
时,它的结构如下:
{
"_id" : ObjectId("54b9d732bb63bc10ec9bad6f"),
"Name" : "Name-1",
"SubDocsList : [{
"Id" : "54b9e76dbb63bc1890e8761b",
"Name" : "Sub-Name-1"
},....]
}
现在我想检索 main doc Id == 54b9d732bb63bc10ec9bad6f
:
private void getSubDoc(SubDocumentModel oModel)
{
_MainDoc = _db.GetCollection<MainDocumentModel>("MainDoc");
_query = Query<MainDocumentModel>.Where(e => e.Id == oModel.MainDocId);
//Here I get the exception
//Exception: An error occurred while deserializing the SubDocsList property
of class Data.MainDocumentModel: Element 'Id' does not match any field or
property of class Data.SubDocumentModel.
private MainDocumentModel _mainDocModel = _MainDoc.FindOneAs<MainDocumentModel>(_query);
oModel.SubDocsList =
_mainDocModel .SubDocsList .FindAll(
x => x.Name.ToLower().Contains("NameFilter"));
//Pagination
oModel.SubDocsList =
oModel.SubDocsList .Take(oModel.ItemsPerPage)
.Skip(oModel.ItemsPerPage*(oModel.PageNo - 1))
.ToList();
}
为什么我会遇到 反序列化 异常。我做错了什么?
在这里,我将分页逻辑应用于检索到的 c# 列表。我如何在 mongo 查询中应用 pagination 本身?
为主文档应用分页非常容易:
mainDocCursor = MainDoc .Find(_query).SetSortOrder(SortBy.Ascending("Name")).SetLimit(oModel.ItemsPerPage).SetSkip(oModel.ItemsPerPage * (oModel.PageNo - 1));
如何实现子文档相同的分页?
我通过以下小改动解决了反序列化异常:
在子文档模型中:
Public class SubDocumentModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
.
.
.
}
当我插入子文档时,我将 Id 保存为对象 ID 而不是字符串(之前我将其保存为导致问题的字符串):
public void addSubDocument(SubDocumentModeloModel)
{
_MainDoc = _db.GetCollection<MainDocumentModel>("MainDoc");
BsonDocument subDocument = new BsonDocument
{
{"_id", ObjectId.GenerateNewId()},//Previously it was {"Id", ObjectId.GenerateNewId().ToString()}
{"Type", oModel.Name}
};
_query = Query<MainDocumentModel>.Where(e => e.Id == oModel.MainDocId);
_updateBuilder = Update.Push("SubDocsList", subDocument);
_MainDoc.Update(_query, _updateBuilder);
}
但是我还没有找到在 mongodb-c#
中对子文档进行分页的方法