Skip to content

Instantly share code, notes, and snippets.

@spolat
Last active September 1, 2018 16:53
Show Gist options
  • Save spolat/2f646480ebd33980f39457a826d12a7e to your computer and use it in GitHub Desktop.
Save spolat/2f646480ebd33980f39457a826d12a7e to your computer and use it in GitHub Desktop.
// Books Schema
var bookSchema = new Schema({
name: String,
postedBy: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
dateCreated: Date,
});
// Registering Schema
var Book = mongoose.model('Books', bookSchema);
// User Schema
var userSchema = new Schema({
fullName: String,
age: Number,
books: {type: mongoose.Schema.Types.ObjectId, ref: 'Books'
});
// Registering Schema
var User = mongoose.model('User', userSchema);
Book.findOne({_id: 123})
.populate('users')
.exec(function(err, post) {
// code here
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment