开发者

How can I access related documents within the map function in CouchDb?

Is it possible to get load values from related documents and emit them as part of the key/value along with values from the current document in CouchDb?

Example:

{Id: 1, Type: Entity, Name: US, Code: 001}
{Id: 2, Type: Entity, Name: Alaska, Code: AL, Parent_Id: 1}
{Id: 3, Type: Entity, Name: California, Code: CAL, Parent_Id: 1}
{Id: 4, Type: Entity, Name: Juneau, Code: C-JUN, Parent_Id: 2}

I would want to read the code, name, etc. from the parent entity and 开发者_如何学Pythonthe current entity both and emit them as the key/value in the map function. If the parent has a further parent, I would like to read values from that too. (Link walking in other databases)


In map-reduce, if you emit() an object with an _id in it, then that id will be the document you get when you query with ?include_docs=true.

Thus, when map runs on California, it would do emit(key, {_id: doc.Parent_id, entity: doc})

If you query the view with ?include_docs=true, every row will have a value.entity for the document about California, but also a doc value with the document of the US.

Two restrictions:

  1. You can only query 1 level deep per query in this way.
  2. TO use the include_docs trick, you must know the _id of the linked document when you emit()

Another option for this is view collation, where all related or similar documents are emitted so that they are all ajacent in the view. (Usually you emit a key of the id of one document and the Parent_Id of all its children, so they all fall under the same key in the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜