【发布时间】:2018-07-23 07:57:36
【问题描述】:
我正在构建我的第一个 NodeJS 应用程序,但我尝试创建的查询遇到了死胡同。
const userSchema = new Schema({
email:{
type: String,
},
friends: [{
name: String,
}],
name: {
type: String,
}
});
我的目标是从朋友数组中的每个对象中检索每个名称,并将它们显示给用户。
我已经尝试过:(还有更多)
const friendNames = await User.find(
{_id: req.user._id},
{ friends: { name: String } }
)
const friendNames = await User.find(
{_id: req.user._id},
{ friends: { $elemMatch: { name: String } }}
)
const friendNames = await User.find(
{_id: req.user._id},
{ friends: { name: {$exists : true} } }
)
我认为这将是一个非常简单的查询,但我要么忽略了某些东西,要么完全误解了一个概念。这是我的第一个主要障碍。任何帮助或指导将不胜感激。
【问题讨论】:
-
类似
const friendNames = await User.find( {_id: req.user._id}, { friends:1 } )
标签: arrays node.js mongodb mongodb-query