我的模型的架构是:-
var socialSchema = new mongoose.Schema({
friend_names:[String],
friend_pics:[String]
})
var userSchema= new mongoose.Schema({
name: {
type: String,
},
username: {
type: String,
index:true
},
email: {
type: String,
},
password: {
type: String,
},
social:[socialSchema]
})
考虑以下猫鼬查询:-
module.exports.friendAccept= function(req, res){
var friend_name= req.body.frnames;
var friend_pic = req.body.frpics;
var user= req.user;
User
.findOne({name:user.name})
.select('social')
.exec(function(err,user){
console.log(user);
})
}
上面的控制台日志的输出看起来像这样:-
{ _id: 5bc6205168bf4e52a8c1aa26,
social:
[ { friend_names: [Array],
friend_pics: [Array],
_id: 5bc73e145d2a2d3808f3cd71 } ] }
{ _id: 5bc6205168bf4e52a8c1aa26,
social:
[ { friend_names: [Array],
friend_pics: [Array],
_id: 5bc73e145d2a2d3808f3cd71 } ] }
上面声明的friend_name和friend_pic变量包含这些子文档之一中存在的值。我如何获取子文档的_id(例如5bc6205168bf4e52a8c1aa26或5bc73e145d2a2d3808f3cd71的示例),该子文档实际上保存着这些值friend_name和friend_pic