如何从mongoose查询中检索值

问题描述 投票:1回答:1

我是nodejs和mongoose的初学者。我正在尝试检索astname有笔记本电脑的类别值。请建议我如何实现这一目标。

var astname = "Laptop";

Asset.find({ astname : astname}, function(err, foundcategory){
       if(err){
           console.log(err);
       } else {
           console.log(foundcategory.category);
       }
   })
node.js mongodb mongoose
1个回答
0
投票

foundcategory在你的例子中是一个数组。使用findOne而不是find来查询单个文档。

var astname = "Laptop";

Asset.findOne({ astname : astname}, function(err, foundcategory){
   if(err){
       console.log(err);
   } else {
       console.log(foundcategory.category);
   }
})
© www.soinside.com 2019 - 2024. All rights reserved.