MongoDB 数组对象更新和页面
我有一个存储在 MongoDB 中的实体类,看起来类似于:
public class Theme
{
public ObjectId Id { get; set; }
public string Description { get; set; }
public string Title { get; set; }
public List<Comment> CommentList { get; set; }
}
public class Comment
{
public string Content { get; set; }
public string Creator { get; set; }
public string Date { get; set; }
}
使用 MongoDB C# 驱动程序,并基于模型,我如何解决以下问题:
- 更新主题的注释,例如:
theme1。 CommetList[10].Creator = "Jack"
- 如何分页数组对象
谢谢。
Wentel
@Andrew Orsich
感谢您的帮助。
还有一个麻烦:
var query = Query.EQ("Id", id); //The 'id' type is ObjectId
List<Theme> newDatas = themeCollection.FindAs<Theme>(query).ToList();
Theme newData = themeCollection.FindOneByIdAs<Theme>(allDatas[0].Id);
结果:'newDatas'为空而'newData'有数据,为什么?
I've a entity class stored in MongoDB that looks similar to these:
public class Theme
{
public ObjectId Id { get; set; }
public string Description { get; set; }
public string Title { get; set; }
public List<Comment> CommentList { get; set; }
}
public class Comment
{
public string Content { get; set; }
public string Creator { get; set; }
public string Date { get; set; }
}
Using the MongoDB C# driver,and based on the model, how can i resolve for the following questions:
- Update a comment of the theme, eg:
theme1.CommetList[10].Creator = "Jack"
- How to page for the array object
Thanks.
Wentel
@Andrew Orsich
Thanks for your help.
And there is another trouble:
var query = Query.EQ("Id", id); //The 'id' type is ObjectId
List<Theme> newDatas = themeCollection.FindAs<Theme>(query).ToList();
Theme newData = themeCollection.FindOneByIdAs<Theme>(allDatas[0].Id);
Result: 'newDatas' is null and 'newData' has data, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1.使用 位置运算符:
您可能还需要将 id 添加到 Comment 类。在这种情况下,您可以通过查询注释进行更新匹配,如下所示:
2.您可以加载整个主题并使用 linq 等从 C# 进行注释分页。或者您也可以使用 $slice ,如下所示:
1.Using positional operator:
Also you probably need to add id to the Comment class. In this case you can updated matched by query comment like this:
2.You can load entire theme and do paging of comments from c# using linq for example. Or you can also use $slice like this:
对于第二个问题,您需要执行以下操作:
For your second question you need to do the following: