我有模式:如何從Mongoose數組中的每個對象中只選擇一個字段?
{
name: String,
surname: String,
note: String,
someField: String,
secondSomeField: String
blackList: [{
userId: mongoose.Types.ObjectId,
reason: String
}]
}
我需要的所有字段選擇文件,但在黑名單領域,我需要只選擇用戶ID。示例我想要的:
{
name: "John",
surname: "Doe",
note: "bwrewer",
someField: "saddsa",
secondSomeField: "sadsd",
blackList: [58176fb7ff8d6518baf0c931, 58176fb7ff8d6518baf0c932, 58176fb7ff8d6518baf0c933, 58176fb7ff8d6518baf0c934]
}
我該如何做到這一點?當我做
Schema.find({_id: myCustomUserId}, function(err, user) {
//{blackList: [{userId: value}, {userId: value}]}
//but i need
//{blackList: [value, value, value]}
})
它的回答是: 'Schema.aggregate( {$比賽:{_id:mongoose.Types.ObjectId(myCustomId)}},{ $項目:{黑名單: 「$ blackList.userId」,名稱:true,surname:true,someField:true}}).exec(fn)' – Jackson