Question

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. 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. So I just pass in the email field, it,s value, the doc id and its corresponding rev. But when I do such an update, my doc now will consist of only the E-mail. How do I restrict this., i.e, only the email id gets updated keeping the remaining fields and it's value intact. I am using jquery.couch.js

Was it helpful?

Solution

Perhaps you are doing it all wrong... you are trying to solve the problem in CouchDB in non-CouchDB way. 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:

Post document: { _id: "post1", type: "post", ... } Comment document:

{ 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.

Same approach is described in details here.

OTHER TIPS

So I just pass in the email field, it,s value, the doc id and its corresponding rev.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top