[英]Unable to retrieve subdocument items in mongodb c#
Model structure: 型号结构:
//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; }
.
.
.
}
In DB when I insert MainDocuement
, its structure looks like: 在DB中,当我插入MainDocuement
,其结构如下所示:
{
"_id" : ObjectId("54b9d732bb63bc10ec9bad6f"),
"Name" : "Name-1",
"SubDocsList : [{
"Id" : "54b9e76dbb63bc1890e8761b",
"Name" : "Sub-Name-1"
},....]
}
Now I want to retrieve subdocuments where main doc Id == 54b9d732bb63bc10ec9bad6f
: 现在我想检索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();
}
Why I am getting above deserializing exception. 为什么我超越了反序列化异常。 What I am doing wrong? 我做错了什么?
Here I am applying the pagination logic to the retrieved c# list. 在这里,我将分页逻辑应用于检索到的c#列表。 How I can apply pagination itself in mongo query? 我如何在mongo查询中应用分页本身?
It's very easy to apply pagination for main doc as: 为主要文档应用分页非常简单:
mainDocCursor = MainDoc .Find(_query).SetSortOrder(SortBy.Ascending("Name")).SetLimit(oModel.ItemsPerPage).SetSkip(oModel.ItemsPerPage * (oModel.PageNo - 1));
How can I achieve the same pagination for subdocuments? 如何实现子文档的相同分页?
I solved the exception of deserializing, with following small changes: 我做了以下小的更改解决了反序列化的异常:
In SubDocumentModel: 在SubDocumentModel中:
Public class SubDocumentModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
.
.
.
}
And when I insert the subdocument, I am saving the Id as object id not as a string (previously I was saving it as a string which was causing the problem): 当我插入子文档时,我将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);
}
However I've still not found a way for pagination of subdocument in mongodb-c# 但是我仍然没有在mongodb-c#中找到分页的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.