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

Mongoose populating path with multiple subpaths

Imagine i have the following models: # MODEL A schemaA = mongoose.Schema _bId: type: mongoose.Schema.Types.ObjectId ref: "B" # MODEL B schemaB = mongoose.Schema _cId: type: mongoose.Schema.Types.ObjectId ref:…
Gbeschbacher
  • 428
  • 8
  • 18
6
votes
1 answer

Mongoose deep populating multiple 2nd level objects not working

I have a model A wit this field : var field = { foo: String, b: [{ type: Schema.Types.ObjectId, ref: 'B' }] } and model B with this feilds : var field = { c: { type: Schema.Types.ObjectId, ref: 'C' } // let's say this has 3 string…
jofftiquez
  • 7,548
  • 10
  • 67
  • 121
5
votes
1 answer

Can't get mongoose virtual populate up and running in NestJS app

I am currently working on a project where virtual populates are quite necessary: There's a customer scheme, a document scheme as well as some others referencing the customer. Inside the customer I want to populate the referenced items. I already had…
Janik
  • 101
  • 1
  • 6
5
votes
2 answers

How do I populate a newly created mongoose Document?

I have a case where I am checking whether a document already exists and if it does not exists I am creating a new document. I need to populate 2 fields inside the document. My problem is that the .populate method is not supported on the .create…
5
votes
3 answers

Mongoose populate does not populate array

I have struggled with the mongoose.model.populate function for hours now. I have even tried directly copying and pasting several solutions without luck. I have a User model which is supposed to contain an array of 'Dilemmas' which he/she has…
5
votes
2 answers

Mongoose how to filter on populate field

I have Orders and Users and i want to find orders by User phone number via Mongoose Populate or something else how do i do that ? Order (_id, item, user_id) User (_id, email, phone) const orders = await Order.find({}).populate({path: 'user',…
Khanakia
  • 723
  • 1
  • 8
  • 20
5
votes
3 answers

populate following users mongoose

Lemme take time to explain what is happening from start to finish. Preamble: A user a follows 10 other people. When user A logs in, an X number of posts from each of the 10 people are pulled into view. I do not know if it is the right thing to do,…
KhoPhi
  • 9,660
  • 17
  • 77
  • 128
5
votes
1 answer

Is it possible to have mongoose populate a Mixed field only if the field contains an ObjectId?

Setup Let's say we have schemas for Foo and Bar. Foo has a field bar. This field can either contain an ObjectId that reference a document Bar, or it could contain other arbitrary objects that are not ObjectIds. const fooSchema = new…
Zsw
  • 3,920
  • 4
  • 29
  • 43
5
votes
1 answer

Mongoose populate() returns empty array

I want to query the subdocument array by the property 'token' in clientSchema. But I'm not able to populate the subdocument array. It always returns empty value. This is what I'm tried var performAuthAsync = promise.promisify(performAuth); var…
MohanRajNK
  • 895
  • 2
  • 13
  • 26
5
votes
0 answers

Deep Populate With Condition

I have a Message Collection which contains messages, models look like this. var MessageSchema = new mongoose.Schema({ groupId: { type: Schema.ObjectId, ref: 'Group' }, }); var GroupSchema = new mongoose.Schema({ type: String, …
Rijad Rahim
  • 409
  • 4
  • 12
5
votes
1 answer

Why can't I access a mongoose schema's method?

I have this Mongoose schema in a Nodejs application: const mongoose = require('mongoose'), Schema = mongoose.Schema, sodium = require('sodium').api; const UserSchema = new Schema({ username: { type: String, required:…
The E
  • 697
  • 1
  • 9
  • 23
5
votes
2 answers

find(...).populate is not a function in mongoose

I am trying to populate two tables in mongoose and node and I receive the error that populate is not a function. I have search and in the documentation it seems that it does the same as I. Here the model: var mongoose = require('mongoose'); var…
5
votes
1 answer

How to remove object taking into account references in Mongoose Node.js?

This is my MongoDB schema: var partnerSchema = new mongoose.Schema({ name: String, products: [ { type: mongoose.Schema.Types.ObjectId, ref: 'Product' }] }); var productSchema = new mongoose.Schema({ …
DiPix
  • 5,755
  • 15
  • 61
  • 108
5
votes
0 answers

modifying results in post find mongoose hook

I am trying to find a way to modify the query results of mongoose. Below is the self contained model with the post hook 'use strict'; // load the things we need var mongoose = require('mongoose'); var invoice_db =…
Uma Maheshwaraa
  • 563
  • 2
  • 9
  • 17
5
votes
2 answers

Mongoose, Deep Population on array model

I would like to deep populate a perhaps overcomplicated model var ParentSchema = new Schema({ childs: [{type:Schema.ObjectId, ref: 'Child'}], }); var ChildSchema = new Schema({ subject: [{ price: {type: Number}, data: {type:…
Maxim
  • 3,836
  • 6
  • 42
  • 64
1 2
3
68 69