我正在使用NodeJS和MongoDb作为后端服务。我试图根据条件检索不同的数据,例如我想获取订单状态为待定的名称:
const coll = client.db("My_db").collection("Orders");
coll.distinct("name",{order_status:"pending"},(function(err,docs){
let output = docs.map(r => ({'name':r.name}));
res.send(output);
}));
但是它正在发送像这样的空数组:
[
{},
{},
{},
{},
{},
{}
]
有人请让我知道我在做什么错。任何帮助将不胜感激。
谢谢
您无需针对该响应运行地图。与众不同的函数返回不同值的数组。尝试console.log(docs)并检查它
因为您已将字段指定为名称,所以不需要使用r.name,只需使用r
您可以参考link,希望这会对您有所帮助:)
如果您的MongoDB高于2.2,并且您想查找多个字段,则可以使用聚合
collection = db.tb;
collection.aggregate([
{"$group": { "_id": { name: "$name", email: "$email" } } }
]);