Mongoose从数组元素中select特定的字段
假设我有这个模式
{ jedi: [{ name:String lightsaber_color:String ]} }
我想要返回所有的,只有他们的名字。 我试过了
Jedi.find({}) .select('jedi.name') .exec(function (err, jedi) { if (err) { console.log("nothing found") } }
它没有返回任何东西,而这个代码返回给我一切。
Jedi.find({}) .select('jedi') .exec(function (err, jedi) { if (err) { console.log("nothing found") } }
我看到jedi是一个数组,所以我认为.select('jedi.name')
可能不适用于这个原因。
什么是正确的语法来做到这一点?
你可以试试这个
Jedi.find({}, {'jedi.name':1}, function (err, jedi) { if (err) { console.log("nothing found") } else{ console.log(jedi); } }