mongoose populate filter
Story. find(...). populate({ path: 'fans', // filtering field, you can use mongoDB syntax match: { age: { $gte: 21 } }, // Explicitly exclude `_id`, see http://bit.ly/2aEfTdB select: 'name -_id' }). exec();
Source: mongoosejs.com
populate example in mongoose
const Live = new Schema( { user_id:{ // this one populate type: Schema.Types.ObjectId, ref: 'User', // User Schema required: true }, title: { type: String, }, category:{ // this one populate type: Schema.Types.ObjectId, ref: 'Category' // Category Schema }, } ) let vid = await Live.find({}) .populate({path:'user_id',select:'name image -_id'}) .populate({path:'category',select:'category -_id',model:Category})
mongoos populate a ref
const storySchema = Schema({ authors: [{ type: Schema.Types.ObjectId, ref: 'Person' }], title: String }); // Later const story = await Story.findOne({ title: 'Casino Royale' }).populate('authors'); story.authors; // `[]`
Source: mongoosejs.com
populate in mongoose
const mongoose = require('mongoose'); const { Schema } = mongoose; const personSchema = Schema({ _id: Schema.Types.ObjectId, name: String, age: Number, stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }] }); const storySchema = Schema({ author: { type: Schema.Types.ObjectId, ref: 'Person' }, title: String, fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }] }); const Story = mongoose.model('Story', storySchema); const Person = mongoose.model('Person', personSchema);
Source: mongoosejs.com
mongoose + populate
app.get('/movies', passport.authenticate('jwt', { session: false }), (req, res) => { Movies.find() .populate('Genre') .populate('Actor') .populate('Director') .then((movies) => { res.status(201).json(movies); }) .catch((err) => { console.error(err); res.status(500).send(`Error: ${err}`); }); });
Source: stackoverflow.com