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
5
votes
1 answer

Mongoose.js - population and additional fields?

I want to add some additional data to UserModel like watchedMovies and I have following schema: let userSchema = new Schema({ watchedMovies: [{ type: Schema.Types.ObjectId, ref: 'Movie' }] }) and: let movieSchema = new Schema({ title:…
Vytautas Butkus
  • 5,365
  • 6
  • 30
  • 45
4
votes
1 answer

Mongoose populate first and then filter based on populated field

I have two collections (items, and subitems). The items collection has an object reference to subitems collection in subItems property (It's an array of subItem ObjectIds). I want to build a query where I can first populate the subItems, and then…
Dino
  • 7,779
  • 12
  • 46
  • 85
4
votes
0 answers

Mongoose custom Schema/Type with virtual populate works with Model.populate but not Document.populate

In the vein of mongoose-uuid2, I am trying to create a custom SchemaType for replacing the regular _id with UUID types. The goal is to create not just a SchemaType, but a related Type class as well, and mimic how ObjectId is actually a type. So I've…
Mikey A. Leonetti
  • 2,834
  • 3
  • 22
  • 36
4
votes
2 answers

mongoose virtual populate infinite loop

I'm trying to populate a tag with the tutorials that are related to it, when I use .populate() on the query it works, but when I do it directly on the model I have an inifite loop. Here's my code: Tag.js const mongoose = require("mongoose"); const…
4
votes
1 answer

Mongoose populate single item in array

I have a model that has an array of dynamic references. var postSchema = new Schema({ name: String, targets: [{ kind: String, item: { type: ObjectId, refPath: 'targets.kind' } }] }); I am using the targets property to store…
kajan
  • 152
  • 1
  • 10
4
votes
3 answers

Mongoose virtual populate and aggregates

I'm trying to do aggregations on Mongoose schemas that use the newer virtual populate functionality (using Mongoose 4.13, Mongo 3.6). Lets say I have the following (simplified for illustration purposes) schemas: const ProjectSchema = new…
Ramon
  • 73
  • 2
  • 7
4
votes
2 answers

Bulk insert in MongoDB with mongoose for multiple collections

I have 2 collections(data, metaData) data schema is { _id: ......, name: ......, //not unique mobile: ......, // unique or null email: ......, // unique or null uniqueId: ......, // unique or null } at least one of unique data is required for…
Gaurav Kumar Singh
  • 1,550
  • 3
  • 11
  • 31
4
votes
0 answers

mongoose populate and sort nested item

I have a simple model: user = { 'items': [{ 'name': 'abc', 'pages': [ObjectId("58c703a353dbaf37586b885c"), ObjectId("58c703a353dbaf37586b885d"), ..]} }] }; I'm trying to sort the pages of current item: User.findOne({'_id': id},…
dontHaveName
  • 1,899
  • 5
  • 30
  • 54
4
votes
1 answer

TypeError: populate(...).exec is not a function

I largely believe this error is due to the object I'm calling not containing the .populate function, although I have no idea how to change this to work. To start with, here is the error in full. TypeError:…
4
votes
1 answer

Mongoose populating array of subdocuments

Apologies if this has already been asked, my searches did not turn up the same situation. I have two schemas something like the below: var experimentSchema = new mongoose.Schema({ name : 'string' elements : [{ type :…
Joe
  • 1,455
  • 2
  • 19
  • 36
4
votes
1 answer

Mongoose Deep Populate limiting intermediate model

I am using MongooseDeepPopulate package for the project. I have SchemaA, SchemaB, SchemaC, SchemaD. My SchemaD, SchemaC are connected to SchemaB and SchemaB is connected to SchemaA. I have done like this. var deepPopulate =…
Sankalp
  • 1,300
  • 5
  • 28
  • 52
4
votes
1 answer

Reverse populate mongoose

I'm trying to figure out how to populate my business orders using the businessId property in my orders collection. I tried this but can't get it work. https://www.npmjs.com/package/mongoose-reverse-populate Any idea of what I'm doing wrong or other…
Justin Young
  • 2,393
  • 3
  • 36
  • 62
4
votes
0 answers

How to dynamically populate mongoose document reference at runtime?

I have a schema that has a field that could reference different schema. var HistorySchema = new Schema({ type: {type: String, required: true}, objectId: { type: Schema.Types.ObjectId, required: true, }, changed: {type:…
yuklai
  • 1,595
  • 1
  • 14
  • 26
3
votes
1 answer

Show only requested field in MongoDB from same collection(Node.js)

I have a sample collection as following in mongodb database: { "merchantId": { "$oid": "62c950dfc96c2b690028be88" }, "contactInfo": { "name": "Claudia Shields", "phoneNumber": 8904672101 }, "location": { "address": "737…
Talc
  • 109
  • 6
3
votes
2 answers

toJSON method is ignored when populating sub documents

I need to remove certain fields from the JSON response. I've used the toJSON() method for doing this. Here is the code of my modal method for it. User.methods.toJSON = function () { let obj = this.toObject() delete…
Nik Lakhani
  • 207
  • 1
  • 10