These are chat archives for LearnBoost/mongoose

23rd
Apr 2017
Hajnal
@hyhajnal
Apr 23 2017 15:43
hello. I met a problem about deep populate with mongoose
voteSchema.statics.detail = async function(voteId){
  const vote = await this
    .findById(voteId)
    .populate('user')
    .populate({
      path: 'comments',
      populate: { 
        path: 'childs'
      }
    })
    .populate({
      path: 'comments',
      populate: { path: 'from' }
    })
    .populate({
      path: 'comments',
      populate: { path: 'to' }
    })
  return vote;
}
Hajnal
@hyhajnal
Apr 23 2017 15:48
const voteSchema = new Schema({
  title: String,
  desc: String,
  create_time: Date,
  active_time: Date,
  tag: String,   
  view: Number, 
  msg: Number,
  follow: Number,
  is_voted: Number,
  user: {type: ObjectId, ref: 'User'}, 
  complex: Boolean, 
  votelist: Array,
  comments: [{type: ObjectId, ref: 'Comment'}]
});
const commentSchema = new Schema({
  content: String,
  time: Date,
  star: Number,
  msg: Number,
  voteId: {type: ObjectId, ref: 'Vote'},
  from: {type: ObjectId, ref: 'User'},
  to: {type: ObjectId, ref: 'User'},
  childs: [ {type: ObjectId, ref: 'Comment'} ],
});
I want to find a comment-reply result with childs ref filled , like recursion or deep population, any one can help?
Hajnal
@hyhajnal
Apr 23 2017 15:57
"comments": [
      {
        "_id": "58f777f4eaba472d05e56ab9",
        "content": "养布与音素算明文造严办步知人方。身改世而声查政每向海位速向期约。广指七基本权派证参后性共矿我不整但。",
        "star": 461,
        "msg": 92,
        "time": null,
        "from": {
          "_id": "58fc03c2b78b45f01353b058",
          "name": "武秀英",
          "psd": "etyqe5034728069932406",
          "avator": "http://dummyimage.com/6*6/f27987/79abf2&text=Mock",
          "following_count": 65,
          "follower_count": 31,
          "topic_count": 0,
          "vote_count": 61,
          "vote_join_count": 37,
          "reply_count": 19,
          "follower_count_new": 1,
          "reply_count_new": 4
        },
        "__v": 10,
        "to": {
          "_id": "58fc03c2b78b45f01353b052",
          "name": "范静",
          "psd": "qjhvo3584582169318150",
          "avator": "http://dummyimage.com/6*6/79c4f2/e7f279&text=Mock",
          "following_count": 48,
          "follower_count": 14,
          "topic_count": 7,
          "vote_count": 36,
          "vote_join_count": 42,
          "reply_count": 47,
          "follower_count_new": 9,
          "reply_count_new": 7
        },
        "voteId": "58fb8bd8116553bae1b54d95",
        "childs": [
          {
            "_id": "58f777f4eaba472d05e56ab9",
            "content": "养布与音素算明文造严办步知人方。身改世而声查政每向海位速向期约。广指七基本权派证参后性共矿我不整但。",
            "star": 461,
            "msg": 92,
            "time": null,
            "from": "58fc03c2b78b45f01353b058",
            "__v": 10,
            "to": "58fc03c2b78b45f01353b052",
            "voteId": "58fb8bd8116553bae1b54d95",
            "childs": [
              "58f777f4eaba472d05e56ab9",
              "58f777f4eaba472d05e56aba",
              "58f777f4eaba472d05e56ab8",
              "58f777f4eaba472d05e56ab9",
              "58f777f4eaba472d05e56abc",
              null,
              "58f777f4eaba472d05e56abd",
              "58f777f4eaba472d05e56abd",
              "58f777f4eaba472d05e56aba",
              "58f777f4eaba472d05e56abc"
            ]
          }]