Map/reduce 在 CouchDB 中,如果 id 包含子字符串,则取一个字段

Map/reduce in CouchDB take a field if id contains a subString

我来说明一下:我有这个功能

function (doc) {
  if(doc.MovieId == "1721")
  emit(doc.Rating, 1);
}

但它 return 我一些不相关的文档(例如,它们没有评级字段)。我的文档 _id 由 partitionName:id 组成,所以我想做 if(doc.MovieId == "1721" && doc._id.contains("ratings"){...} 但它不起作用。 有办法吗?

-----编辑 1----- 圈子里的文档不相关。 您需要 JSON 文档的架构吗?

-----编辑 2----- 以下文件不相关 1.

{
  "_id": "movies : 1721",
  "_rev": "1-d7e0e3c8152d6978073d280e0aef7457",
  "MovieId": "1721",
  "Title": "Titanic (1997)",
  "Genres": [
    "Drama",
    "Romance"
  ]
}

2.

{
  "_id": "tags : 1490",
  "_rev": "1-14c20c9cfb3ee1964a298777f80333d5",
  "MovieId": "1721",
  "UserId": "474",
  "Tag": "shipwreck",
  "Timestamp": "1138031879"
}

3.

{
  "_id": "tags : 2791",
  "_rev": "1-e4d6c9573fcdae726a69d5fc6255de27",
  "MovieId": "1721",
  "UserId": "537",
  "Tag": "romance",
  "Timestamp": "1424141922"
}

像这样的文档是相关的:

{
  "_id": "ratings : 31662",
  "_rev": "1-446665286337faaf51e23e40b527ec2d",
  "MovieId": "1721",
  "UserId": "219",
  "Rating": "0.5",
  "Timestamp": "1214043346"
}

以下视图应该只发出 _id 以 "ratings :":

开头的文档
function (doc) {
  var id_prefix = "ratings :";

  if(doc._id.substr(0, id_prefix.length) === id_prefix && doc.MovieId == "1721")
  emit(doc.Rating, 1);
}