更新 MongoDB 中的子文档
现在,我正在 ASP.NET 中的 MongoDB 上工作,刚刚浏览了一篇文章,其中采用了一篇博客文章示例,并提到了以下更新评论的步骤(子文档)。
- 博客被视为主文档
- 评论被视为子文档并与主文档一起发布
- 当我们需要向特定博客插入新评论时,我们将获取现有博客文档,在前面添加新评论(子文档) -结束并再次使用博客 ID 更新博客文档。
如果是这样的话,让我们以非常流行的博客场景为例,其中有数千条评论发布到博客上。如果需要发布任何新评论,我们需要将所有现有评论从 DB 服务器获取到 Web 服务器,这会增加网络流量。
这是一般情况还是有其他方法可以向子文档添加新内容?意味着,我需要一个应该在现有文档中插入新值的功能。
Right now I am working on MongoDB in ASP.NET and just gone through an article in which it has taken a blog post example and mentioned the following steps to update comments (sub document).
- The blog was taken a main document
- Comments were taken as sub document and posted along with main document
- When we need to insert a new comment to a particular blog, we are getting the existing blog document, adding new comment (sub document) in front-end and again updating the blog document with blog ID.
If it is the case, let us take very popular blog scenario where thousands of comments were posted to a blog. If any new comment has to be posted, we need to get all the existing comments from DB server to Web server as a result it will increase network traffic.
Is this general scenario or is there any alternative way to add new content to sub document? Means, I need a functionality which should just insert new values in existing document.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MongoDB 支持“push”命令。 push 命令 将向现有数组追加一个项目。
MongoDB supports a "push" command. The push command will append an item to an existing array.
有一个 $push 运算符,用于将指定的值附加到数组中。假设我们有以下博客文章架构。帖子中已经有两条评论,如果您想向帖子中添加另一条评论,可以使用 $push 运算符。我编写了一个示例代码,将注释插入到节点中的以下架构中,但您也可以在任何 .NET 中使用该逻辑
{
"_id" : ObjectId("55d58d05471d5cc42aaaef1b"),
"title" : "如何在 MongoDB 中删除集合?",
“作者”:“dp123”,
"body" : "身体部分......!!!",
"permalink" : "How_to_drop_a_collection_in_MongoDB",
“标签”:[
“MongoDB”,
“蒙神”,
“蒙戈”
],
“评论” : [
{
“作者”:“DP”,
"body" : "太棒了......",
"email" : "[电子邮件受保护]"
},
{
“作者”:“约翰”,
"body" : "这篇文章对我来说非常有用。自上周以来我一直没有得到解决方案,但我终于得到了它。非常感谢!!!",
"email" : "[电子邮件受保护]"
}
],
“日期”:ISODate(“2015-08-20T08:17:09.541Z”)
//
我们可以在 http: /docs.mongodb.org/manual/reference/operator/update/push/
There is a $push operator which is used to append the specified value to an array. Suppose we have the following schema for blog post. There are already two comments in the post and if you want to add another comment to the post you can use the $push operator. I have written a sample code to insert a comment to the below schema in node but you can use the the logic in any .NET too
{
"_id" : ObjectId("55d58d05471d5cc42aaaef1b"),
"title" : "How to drop a collection in MongoDB?",
"author" : "dp123",
"body" : "body part goes ........!!!",
"permalink" : "How_to_drop_a_collection_in_MongoDB",
"tags" : [
"MongoDB",
"Mongod",
"mongo"
],
"comments" : [
{
"author" : "DP",
"body" : "awesome ......",
"email" : "[email protected]"
},
{
"author" : "John",
"body" : "This article is really useful for me. I am not getting solution since last few week but finally I got it. Thank you very much!!!",
"email" : "[email protected]"
}
],
"date" : ISODate("2015-08-20T08:17:09.541Z")
}
We can get the detailed about the $push at http://docs.mongodb.org/manual/reference/operator/update/push/