自动增量是不可能的。然而,这就是我所做的。
假设您正在从表单中发布内容。
你收到它是这样的
const fromform = req.body.fieldname;
然后在添加它并增加 _id 之前,您可以调用 alldocs 方法。
database.allDocs({include_docs: true,descending: true,limit:1}).then(function(result){
// the rest of the code goes here
});
然后您可以使用结果进行迭代并为其添加一个数字
result.rows.map(function(item) {
var pusher=JSON.stringify(item.id, null, 4);
var idf=parseInt(pusher.slice(1,-1),10)
var addidf=idf+1;
var sid=addidf.toString();
doc = {
_id : sid,
name: 'field_from_form',
tagline : fromform // this is also field from form
}
database.put(doc, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Document created Successfully");
res.send("Collection Updated" + JSON.stringify(doc));
}
console.log("The new array in add tag is pushed is : "+addidf);
});
});
}, function(error) {
res.status(400).send(error);
结果行将为您提供所有文档,但是我们已按降序对其进行排序并将其限制为一个。你有一个对象,所以我们对它进行了攻击。默认情况下 pouch 将返回一个字符串,因此我们对其进行解析并切片引号。然后我们必须添加 +1 来增加它。然后小袋将不接受数字,所以你必须再次将其转换为字符串(phew``)/你完成了。
是的,这不是一个好方法,但与小袋的照明速度有关。我想我们可以忽略它。
还有另一种方式。
你也可以调用
db.info().then(info=> console.log(info.doc_count))
上述方法要快得多,并且会为您节省大量开销,但是您需要在一定程度上以 [object Object] 和 [object Promise] 为导向。我相信诺言会引起人们的注意。
一切顺利!!