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
0 answers

MongoDB Text Search find similar words

we have a database with names of wines and beer(s). We're using MongoDB and our document structure for each wine or beer company looks like this: { name: String //this is the company name products: [ { name: String //this is…
2
votes
1 answer

How to combine the results of two different queries with mongoose?

I have 3 models (Project, Organization, User) with the schema show below. I am using mongo version 4.2.0 and Moongoose version 5.1.1. Question Given a specific user, how do I query all the projects they have access to: either via a direct role in…
P-S
  • 3,876
  • 1
  • 29
  • 26
2
votes
3 answers

Why does mongoose return object IDs' in the form of an object with an ID as a Buffer instead of string?

I have a static method in my UnitSchema that finds a unit by a token. It gets passed a token as a parameter and tries to find the unit with a normal findOne method on the Unit model: UnitSchema.statics.findByToken = async function (token) { const…
2
votes
2 answers

Mongoose: reference between models with same schema keys

As a front-end developer, I would like to have some isomorphic object for two mongoose models. Let's say I have a user profile: const profileSchema = new Schema({ firstName: { type: String }, lastName: { type: String }, // example of…
YUzhva
  • 323
  • 2
  • 15
2
votes
2 answers

Mongoose: Populating a nested array of ids

I've been looking the web for a solution and I can't seem to find an answer to my problem. Here's a very similar case, but for some reason my Document still doesn't get populated in my case. Here are the schemas: Customer Schema: var mongoose =…
Martin Carre
  • 1,157
  • 4
  • 20
  • 42
2
votes
1 answer

Mongoose implementation for Document Referenced Relationship in MongoDB

I need to make a aggregation query using document referenced relationship (not with embedded documents). I'm able to make that query using the mongo shell, but struggling with its equivalent implementation using mongoose. I have tried the virtual…
2
votes
0 answers

What is alternative method of virtual populate to count document and what is effect of virtual populate on model?

I want to total count of driver from particular city, so I am using virtual populate, just want to know alternative method of it and any effect of it on model ? const DriverSchema = new Schema({ name: String, band: String, cityId : { …
Pradip Bhuvani
  • 427
  • 1
  • 4
  • 18
2
votes
2 answers

Populate collection inside another collection mongoDB with mongoose nodejs

I'm trying to get all the information from a particular user. I can already populate the collections that it contains, but I'm unable to populate a very important attribute within these collections, the user. I have the following code that reprints…
Rui Rocha
  • 33
  • 7
2
votes
1 answer

Mongoose: Trying to use .virtual method to rename

I have to rename the name of the field when using populate. const CategorySchema = new Schema( { name: { type: String, unique: true }, featured: { type: Boolean, default: true }, image: String, …
Sarmad Shah
  • 3,725
  • 1
  • 20
  • 42
2
votes
2 answers

Match specific value in mongoose populate

Following is the schema of a user collection: const Mongoose = require('mongoose') const Schema = Mongoose.Schema const userSchema = new Schema({ name: { type: String, required: true }, email: { type: String, …
2
votes
1 answer

Mongoose populates if findById(); does not populate if find()

Basically, Mongoose seems to populate an an array if I use findById().populate(), but not if I use find().populate(). I need to use findById()... Any ideas why find().populate() doesn't seem to work? Code snippet (express route): //Retrieve a user's…
daCoda
  • 3,583
  • 5
  • 33
  • 38
2
votes
1 answer

Get posts with n latest comments in MongoDB

So this is a common problem, getting posts with comments, which i can't seem to solve in MongoDB, unlike in MySQL which is easily solved with left-join. Problem: I would like to fetch the latest 8 posts with 2 recent comments on each post in…
Ricky Boyce
  • 1,772
  • 21
  • 26
2
votes
2 answers

How to populate nested entity in Mongoose?

I have such models: const UserSchema = new mongoose.Schema({ name: String, workspaces: [ { workspace: { type: mongoose.Schema.ObjectId, }, owner: Boolean } ] }); const WorkspaceSchema = new mongoose.Schema({ …
b4v
  • 75
  • 1
  • 10
2
votes
1 answer

mongoose populate two simple schemas

I am stuck with mongoose populate() method. Here is my schemas: const GroupSchema = Schema({ title: String, ips: [ { ip: String, hostname: String, added : { type : Date, default: Date.now } } ] }); module.exports =…
Dikobraz
  • 649
  • 2
  • 8
  • 22
2
votes
0 answers

Mongoose: Referencing a subdocument from another subdocument AND population

I have a document model called school which in turn has two subdocuments: students class Each student belongs to one class. var classSchema = new Schema({ name: {type : String} }); var studentSchema = new Schema({ name : {type : String,…