【发布时间】:2018-09-11 15:09:07
【问题描述】:
我正在尝试计算集合中的文档数,然后使用 Node.js 将结果用于另一个插入查询。
var number;
db.collection("collection").countDocuments({}, function(error, numOfDocs){
if(error) throw error;
else{
console.log(numOfDocs);
number = numOfDocs;
}
});
var myobj = { num: "Number_"+number };
db.collection("another_collection").insertOne(myobj, function(err, res)
{
if (err) throw err;
console.log("1 document inserted");
});
集合中插入的值为{ "num" : "Number_undefined" }
如何将第一个查询的结果用于另一个查询?
【问题讨论】: