获取子文档的object_id

时间:2018-10-17 12:30:00

标签: node.js mongodb mongoose

我的模型的架构是:-

var socialSchema = new mongoose.Schema({
  friend_names:[String],
  friend_pics:[String],
  friend_id:[String],
  friend_requests:[String],
  friend_req_profilepic:[String]

})


var userSchema= new mongoose.Schema({
    name: {
      type: String,
    },
    username: {
      type: String,
      index:true
    },
    email: {
      type: String,
    },
    password: {
      type: String,

    },

      profileimage:{
        type:String
    },

    friends:{
      type:Number
    },

    social:[socialSchema]


  })

这是我正在做的猫鼬查询:-(这里的frname和frpic是社交模式中的friend_requests和friend_req_profilepic值)

module.exports.friendAccept= function(req, res){
  var frname= req.body.username;
  var frpic = req.body.userpic;
  var user= req.user;
  User
  .findOne({name:user.name})
  .select('social')
  .exec(function(err,doc){
    console.log(doc)
  })
}

以上控制台日志的输出为:-

{ _id: 5bc6c85305c18b5a646d0fdb,
  social:
   [ { friend_names: [],
       friend_pics: [],
       friend_id: [],
       friend_requests: [Array],
       friend_req_profilepic: [Array],
       _id: 5bc723fbc002735ae4c380aa },
     { friend_names: [Array],
       friend_pics: [Array],
       friend_id: [],
       friend_requests: [],
       friend_req_profilepic: [],
       _id: 5bc72421c002735ae4c380ab } ] }

frname和frpic的上述值属于这些文档之一。因此,如何获取存储这两个值的子文档的_id

0 个答案:

没有答案