有没有办法从Couchdb中获取更干净的JSON?

时间:2019-06-20 05:10:16

标签: json couchdb

我想知道是否有一种方法可以从Couchdb中获取仅使用doc的更干净的JSON,而不是将其放在“ rows”和“ doc”下

这是默认输出

    {
   "total_rows":1,
   "offset":0,
   "rows":[
      {
         "id":"7d9fd5824f9029186c1eec1bda005e75",
         "key":"7d9fd5824f9029186c1eec1bda005e75",
         "value":{
            "rev":"1-f99879f67cfd27685f92c884c236a0fd"
         },
         "doc":{
            "_id":"7d9fd5824f9029186c1eec1bda005e75",
            "_rev":"1-f99879f67cfd27685f92c884c236a0fd",
            "title":"Hello World",
            "messeges":"This is the first messege. Helloo there"
         }
      }
   ]
}

这是所需的输出:

    {
   "_id":"7d9fd5824f9029186c1eec1bda005e75",
   "_rev":"1-f99879f67cfd27685f92c884c236a0fd",
   "title":"Hello World",
   "messeges":"This is the first messege. Helloo there"
}

谢谢

2 个答案:

答案 0 :(得分:1)

查看您的代码会很有用。我怀疑这是alldocs api的输出吗?如果知道所需的文档ID,则可以使用get api来返回所需的JSON。否则,您必须遍历“行”,即

for (x in results.rows) {...}

,然后使用x.doc获取您的JSON。

答案 1 :(得分:0)

谢谢大家的帮助。 我想出了一种通过使用附加到该查询的映射函数在Node js中执行此操作的方法

在nano页面中,他们将其列为

alice.list().then((body) => {
  body.rows.forEach((doc) => {
    console.log(doc);
  });
});

相反,我像这样使用它

alice.list().then((body) => {
  var result = body.rows.map( (x) => {
      return x.doc
  });
   console.log(result);
});

对我有用。

总体上仍保持新状态,直到ouchdb和数据库为止。