MongoDB高级查询帮助
我有这个文档,其中有选民数组
,我得到了帖子 ID (999)、回复 ID (123) 和选民 ID (10) 我怎样才能将第二个选民的名字从鲍勃更改为汤姆?
这可以使用 csharp 驱动程序来完成吗?
posts:
{
_id: ObjectId("999"),
title : "First post",
"replies" :
[
{
"_id" : ObjectId("123"),
"text" : "Great post",
"votes" : 2,
"voters":[
{
id: 10,
name:"bob"
},
{
id: 11,
name: "john"
}
],
}
]
}
编辑:我用更好的文档结构重新编写了问题。
类:我的代码仍然具有与原始结构相关的类。 对于我新提出的结构,我的课程看起来像
public class Post
{
public Post()
{
Id = ObjectId.GenerateNewId();
title = string.empty;
}
[BsonId]
public ObjectId Id { get; set; }
public string title { get; set; }
public List<Reply> replies { get; set; }
}
public class Reply
{
public Reply()
{
Id = ObjectId.GenerateNewId();
text = string.empty;
votes = 0;
}
[BsonId]
public ObjectId Id { get; set; }
public int votes { get; set; }
public string text { get; set; }
public List<Voter> voters { get; set; }
}
public class Voter
{
public Voter()
{
name = string.empty;
}
[BsonId]
public ObjectId Id { get; set; }
public string name { get; set; }
}
I have this document where I have voters array
I am given post id (999), reply id (123) and voter id (10)
How can I change the second voter's name from bob to tom?
Can this be done using csharp driver?
posts:
{
_id: ObjectId("999"),
title : "First post",
"replies" :
[
{
"_id" : ObjectId("123"),
"text" : "Great post",
"votes" : 2,
"voters":[
{
id: 10,
name:"bob"
},
{
id: 11,
name: "john"
}
],
}
]
}
EDIT: I recomposed the question with better document structure.
Classes: My code still has the original structure related class.
For my new proposed structure, this is my classes will look like
public class Post
{
public Post()
{
Id = ObjectId.GenerateNewId();
title = string.empty;
}
[BsonId]
public ObjectId Id { get; set; }
public string title { get; set; }
public List<Reply> replies { get; set; }
}
public class Reply
{
public Reply()
{
Id = ObjectId.GenerateNewId();
text = string.empty;
votes = 0;
}
[BsonId]
public ObjectId Id { get; set; }
public int votes { get; set; }
public string text { get; set; }
public List<Voter> voters { get; set; }
}
public class Voter
{
public Voter()
{
name = string.empty;
}
[BsonId]
public ObjectId Id { get; set; }
public string name { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如安德鲁指出的,你需要改变结构。不知道您对查询还有哪些其他限制,我无法提出明确的建议。但是,如果您将
replies
更改为文档而不是数组,并且键是回复 ID 字符串,您可以执行以下操作(在 MongoDB shell 中):该查询应该很容易根据您的 ID 构造你的 C# 驱动程序。
为了清楚起见,重组后的文档看起来像这样:
如果这对您不起作用,请告诉我们更多有关您的用例的信息,也许我们可以找出可行的方法。
另一种选择是将回复分成自己的集合,使每个回复成为一个文档,其中包含一个引用父帖子的字段。在这种情况下,我怀疑这可能是最简单的解决方案,并且应该具有良好的性能和合理的索引。
As Andrew points out, you need to change the structure. Not knowing what other constraints you have on the querying I can't make a definite suggestion. However, if you change
replies
to a document instead of an array, with keys being the reply ID string, you could do (in the MongoDB shell):This query should be easy to construct from your IDs in your C# driver.
For clarity, the restructured document would look something like:
If that doesn't work for you, tell us a bit more about your use case and maybe we can figure out what would work.
The other alternative is that you break out replies into their own collection, making each reply a document with a field to reference the parent post. In this case I suspect that might be the simplest solution and should have good performance with a sensible index.