Questions tagged [mongoose-populate]

The Mongoose ODM has a populate() feature which lets you reference related documents in other collections. Mongoose can populate a single document, multiple documents, plain object, multiple plain objects, or all objects returned from a query.

MongoDB 3.2+ servers include a $lookup aggregation feature which enables joining query results from multiple collections in the same database.

The Mongoose ODM provides a client-side alternative called populate(), which lets you reference related documents in other collections (including those in different databases).

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s). Mongoose can populate a single document, multiple documents, plain object, multiple plain objects, or all objects returned from a query.

Related Links

1023 questions
57
votes
6 answers

what does populate in mongoose mean?

I came across the following line of code which I couldn't understand ,although there are lot of tutorials that gives information related to examples of populate but there is none that explains what exactly it means.Here is a example var mongoose =…
Sai Ram
  • 4,196
  • 4
  • 26
  • 39
27
votes
2 answers

Can't get the result of Mongoose virtual populate

I have a circle model in my project: var circleSchema = new Schema({ //circleId: {type: String, unique: true, required: true}, patientID: { type: Schema.Types.ObjectId, ref: "patient" }, circleName: String, …
Himanshu Jain
  • 1,809
  • 1
  • 13
  • 23
25
votes
2 answers

How to select specific field in nested populate in mogoose

here is my schema: var UserSchema = new Schema({ email: String, name: String, city: String, username: String, profilePic: String, phoneNo: Number, shortList: { project: [{ type: Schema.ObjectId, ref: "Project"…
Rhushikesh
  • 3,630
  • 8
  • 45
  • 82
24
votes
4 answers

Mongoose populate nested array

Assuming the following 3 models: var CarSchema = new Schema({ name: {type: String}, partIds: [{type: Schema.Types.ObjectId, ref: 'Part'}], }); var PartSchema = new Schema({ name: {type: String}, otherIds: [{type: Schema.Types.ObjectId, ref:…
lostintranslation
  • 23,756
  • 50
  • 159
  • 262
20
votes
3 answers

How does mongoose populate work under the hood

Could somebody tell me how I have a collection a { b: String c: Date d: ObjectId --> j } j { k: String l: String m: String } when I carry out a: a.find({ b: 'thing' }).populate('d').exec(etc..) in the background is this actually carrying…
David
  • 363
  • 3
  • 14
19
votes
4 answers

How to populate nested entities in mongoose?

I have the following mongoose schema structure userSchema = new Schema({ roles: [ role: {type: Schema.Types.ObjectId, ref: 'Role' } ] }) rolesSchema = new Schema({ name: String, roleEntities: [ { entity : {type:…
jozzy
  • 2,863
  • 3
  • 15
  • 12
19
votes
2 answers

Multiple schema references in single schema array - mongoose

Can you populate an array in a mongoose schema with references to a few different schema options? To clarify the question a bit, say I have the following schemas: var scenarioSchema = Schema({ _id : Number, name : String, guns :…
BrentShanahan
  • 681
  • 1
  • 7
  • 17
17
votes
1 answer

MongoDB $lookup vs Mongoose populate

I have seen this and other similar titled questions, none answer my question. I was going through the mongoose documentation where I read MongoDB has the join-like $lookup aggregation operator in versions >= 3.2. Mongoose has a more powerful…
YulePale
  • 6,688
  • 16
  • 46
  • 95
17
votes
2 answers

populate with mongoose pagination

i tried to fetch data using npm mongoose-paginate but populate is not working here is my UsersSchema.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var usersSchema = new Schema({ name : String, created_at : { type :…
Kaushik Makwana
  • 2,422
  • 9
  • 31
  • 50
15
votes
3 answers

How to populate mongoose references in nestjs?

I define a Person and Story schemas : @Schema() export class Person extends Document { @Prop() name: string; } export const PersonSchema = SchemaFactory.createForClass(Person); @Schema() export class…
Yaron
  • 1,655
  • 6
  • 20
  • 38
14
votes
1 answer

lean() inside populate in mongoose

I am populating a user collection in mongoose and want to set a flag if the user is signed up using social accounts. So I am selecting that fields in populate. But due to security concerns I don't want to pass it in response. So I converted it into…
Mariya James
  • 935
  • 2
  • 9
  • 27
13
votes
3 answers

Populating Mongoose objects from id to new field

I was working with mongoose to populate field of ids with their respective documents to a new field.my question is assuming my cart model is - let CartSchema = new mongoose.Schema({ userId: { type: mongoose.Schema.Types.ObjectId, …
Natnael Getachew
  • 137
  • 1
  • 1
  • 9
13
votes
2 answers

Mongoose Populate after Aggregate

I am trying to get a specific data model after I run an aggregate pipeline followed by populate but I am falling just short of it. The desired result in the end is the following: [ { _accountId: "5beee0966d17bc42501f1234", name: "Company…
13
votes
1 answer

How to rename path in response for populate

I have a query like this: galleryModel.find({_id: galleryId}) .populate({ model: 'User', path: 'objectId', select: 'firstName lastName' }) End response for objectId will be…
Vladimir Djukic
  • 925
  • 2
  • 9
  • 28
12
votes
2 answers

Mongoose 'reversed' population, i.e. populating a parent object based on the reference defined in child schema

Let's borrow the excellent example from scaryguy with modification as below: Project Group Schema: var ProjectGroupSchema = new Schema({ projectGroupId : String, title : String }); Project Schema: var ProjectSchema = new…
Eric Xin Zhang
  • 1,230
  • 15
  • 20
1
2 3
68 69