1

Mongoose Populate works great. But could someone use populate to pull in documents by another attribute that isn't the _Id?

For Example Comment Schema:

var CommentSchema = new Schema({
  _id:Auto Generated
  postId:1,
  userId:411,
  comment:'This is a comment',
  // Would want to fetch Like via the userId not likes _id
  likes: {
    type:mongoose.Schema.Types.ObjectId, // This will have to change?
    ref:'Like'
  }
})

and Like Schema:

var LikeSchema = new Schema({
    _Id:Auto Generated,
    blogId:1,
    userId:411,
    postId:1,   
    commentId:1     
});

'likes' within Comment Schema is being set to the userId in this case being '411'. So I assume at the moment from the above Schema the populate will be trying to match likes in CommentSchema being the userId to LikeSchema's _Id. Can I join the two by userId?

Any thoughts by anyone is welcome, Just want to broaden my knowledge of the Populate functionality in Mongoose :)

HireLee
  • 561
  • 1
  • 9
  • 25
  • Short answer is no. Possible dupe of http://stackoverflow.com/questions/19287142/populate-a-mongoose-model-with-a-field-that-isnt-an-id; see the non-accepted answer. – JohnnyHK Mar 15 '16 at 13:33
  • Checked that question/answer before posted this. Thought with the possibility of it being from 2013 that things could have progressed or changed since then. – HireLee Mar 15 '16 at 13:35

1 Answers1

1

left outer join is possible since (actually newest) version of mongodb 3.2 with $lookup pipeline method when using aggregate.

link to documentation

libik
  • 22,239
  • 9
  • 44
  • 87