HireLee
HireLee

Reputation: 573

Mongoose Populate Method Not by _Id

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 :)

Upvotes: 1

Views: 1068

Answers (1)

libik
libik

Reputation: 23029

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

link to documentation

Upvotes: 1

Related Questions