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

Using cuid to replace mongoose ObjectIds

I was thinking of using cuid (https://www.npmjs.com/package/cuid) to replace mongoose's naturally generated objectids, so that _id in my model would get a cuid rather than a generated one. Questions: How do you achieve this Are there any side…
user1775718
  • 1,499
  • 2
  • 19
  • 32
2
votes
2 answers

sort in populate does not work (Mongoose)

My MongoDB version 3.2, mongoose version is 4.6.0 These are my schemas: // chat const chatSchema = new mongoose.Schema({ users: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }], lastMessage: { type:…
Hongbo Miao
  • 45,290
  • 60
  • 174
  • 267
2
votes
1 answer

How to use the function populate() correctly

I used the function .populate() to get the equipements grouped by category, so my model is like this var mongoose = require('../config/db'); var EquipementSchema = mongoose.Schema({ libelle: String, marque: String, category: { type:…
2
votes
1 answer

How to fetch value from one schema and update in other using middlewares in mongoose

While updating Movie documents, get value from User collection and save it with the documents getting updated in Movie. Movie.pre('update', function (next) { var id = this._update.$set.userid; var some = this; User.findOne({userid: id},…
node_saini
  • 727
  • 1
  • 6
  • 22
2
votes
0 answers

using a key value other than Schema.Types.ObjectId in mongoose

I am creating a collection w/n mongo using mongoose to populate related users. I have clinicians and patients. I want patients to have an array of clinicians as they are enrolledwith. Later I'll load a dashboard for clincian based on patient user…
codeangler
  • 779
  • 8
  • 16
2
votes
1 answer

Mongoose Query Help: Paginate, Sort, Limit on Nested Array

I am have a chat Mongoose model in the below is the sample data. If this is still not clear please revert back to me with your questions. Any help is greatly appreciated. { "_id" : ObjectId("5745910831a1sd58d070a8faa"), …
2
votes
1 answer

Mongoose prevent assignment of default value to enum

I have the following property in object defined in Mongoose schema, is there a way to prevent Moongose from assigning default enum value on App.create() or during upserting band_collection: { type: String, enum:…
eugenekgn
  • 1,652
  • 2
  • 17
  • 38
2
votes
1 answer

Fatal error: Schema hasn't been registered for model "a". Use mongoose.model(name, schema)

I am having the following error: Fatal error: Schema hasn't been registered for model "a". Use mongoose.model(name, schema) Here is my file structure: --------a.model.js-------- 'use strict'; var mongoose = require('mongoose'), Schema =…
Mohit
  • 2,189
  • 4
  • 22
  • 40
2
votes
1 answer

Populate multiple level does not work

First = new mongoose.Schema({ name: String, second: {type: Schema.Types.ObjectId, ref: 'Second'}, }); Second = new mongoose.Schema({ name: String, third: {type: Schema.Types.ObjectId, ref: 'Third'}, }); Third = new mongoose.Schema({ …
Sato
  • 8,192
  • 17
  • 60
  • 115
2
votes
0 answers

Keystone js - populateRelated fields appearing as undefined

Every Application has a 1 to 1 relationship with a user. Every Trip has n Applications. Here's my following code for populating all User (students) data in the Applications of all trips: view.on('init', function(next) { Trip.model.find() …
2
votes
0 answers

Populate Aggregation Results of Child Schema in Mongoose

I have the following (simplified) Schema var options = {discriminatorKey:'kind'}; var activitySchema = new Schema( { title:{type:String} },options ); var Activity = mongoose.model('Activity', activitySchema); var dateActivitySchema =…
jeh
  • 2,373
  • 4
  • 23
  • 38
2
votes
1 answer

How to populate an array of sub `ref`ed documents mongoose?

I'm working on a MEAN stack app with Mongoose version 4.4.0 and having a problem with populating an array of sub-documents. (couldn't find solutions elsewhere on SO.) The following illustrates the structure of the models: Visa model var visaSchema =…
2
votes
1 answer

.where() by populated fields

I want to find the closest workers by his location which have a specific skill. Location schema: var schema = Schema({ name: String, type: String, // home | latest user: {type: Schema.Types.ObjectId, ref: 'User'}, address: String, …
Manhhailua
  • 1,062
  • 3
  • 13
  • 33
2
votes
1 answer

Mongoose: how to populate on nested array ( deep-level)

I have a trouble with populate deep level. I searched and read many pages but it can't resolve. Here is my Schema: var boxSchema = new mongoose.Schema({ name: { type: String, unique: true }//, required: true , height: { …
2
votes
1 answer

Mongoose .populate() not populating

I've tried a few different things, and I just can't get Mongoose to populate Users information into the Items collection. File: users.js var mongoose = require( 'mongoose' ) Schema = mongoose.Schema, ObjectId = Schema.Types.ObjectId; var…
unknowndomain
  • 985
  • 1
  • 10
  • 34