简体   繁体   English

在CouchDB中更新文档

[英]Updating doc in CouchDB

I know how to update a document in CouchDB, but when I update a doc, the fields that I didn't specify for the update gets deleted. 我知道如何更新CouchDB中的文档,但是当我更新文档时,未为更新指定的字段将被删除。 How do I restrict the same. 我如何限制相同。 For instance, my CouchDB doc consists of fields: Name,Age,Email and I would like to update only the email field of this doc. 例如,我的CouchDB文档包含以下字段:名称,年龄,电子邮件,而我只想更新此文档的电子邮件字段。 So I just pass in the email field, it,s value, the doc id and its corresponding rev. 因此,我只需要在电子邮件字段中输入它的值,文档ID及其相应的转速。 But when I do such an update, my doc now will consist of only the E-mail. 但是,当我进行此类更新时,我的文档现在仅包含电子邮件。 How do I restrict this., ie, only the email id gets updated keeping the remaining fields and it's value intact. 如何限制此值,即仅电子邮件ID得到更新,保留其余字段且其值保持不变。 I am using jquery.couch.js 我正在使用jquery.couch.js

Perhaps you are doing it all wrong... you are trying to solve the problem in CouchDB in non-CouchDB way. 也许您做错了所有事情……您试图以非CouchDB的方式解决CouchDB中的问题。 Why are you storing all the comments in one document? 为什么将所有评论存储在一个文档中? If you want to retrieve them in one request with the post, then you can use map to aggregate post and comments from separate documents like this: 如果要在一个请求中与帖子一起检索它们,则可以使用map汇总来自单独文档的帖子和评论,如下所示:

Post document: { _id: "post1", type: "post", ... } Comment document: 发布文档: { _id: "post1", type: "post", ... }注释文档:

{ type: "comment",
  post: "post1", // id of the post being commenting
  ...
}

Map: 地图:

function (self) {
  if (!self.type) return;
  if (self.type == "post") {
    emit([self._id, 0], self);
  } else if (self.type == "comment" && self.post) {
    emit([self.post, 1, self.time], self);
  }
}

Retrieve post with comments with sorted by date: 检索带有按日期排序的评论的帖子:

curl http://127.0.0.1:5984/yourdb/_design/yourapp/_view/yourview?startkey=[%22post1%22]&endkey=[%22post1%22,{}]

In this way you can also retrieve only some of the comments and implement paging easily. 这样,您还可以仅检索某些注释并轻松实现分页。 All the comments should will be store in B-tree close to the post, so it should also be efficient. 所有评论都应存储在靠近该帖子的B树中,因此它也应该有效。

Same approach is described in details here . 此处详细介绍了相同的方法。

So I just pass in the email field, it,s value, the doc id and its corresponding rev. 因此,我只需要在电子邮件字段中输入它的值,文档ID及其相应的转速。

You need to save the entire document with the updated values, not just the values you want to update. 您需要使用更新后的值保存整个文档,而不仅仅是要更新的值。 Think of the document itself as one big composite value; 将文档本身视为一大综合价值; you can't change part of it without changing the entire thing. 您不能更改其中的一部分而不更改整个内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM