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

How to return an array of document contents when populate is involved with mongoose

Let's assume I have mongoose models for books and pages like this: mongoose.model("Book", new Schema({ title: String }); and this mongoose.model("Page", new Schema({ pageNumber: Number, _bookId: {type: ObjectId, ref: "Book"} }); Every…
Florian
  • 712
  • 5
  • 18
3
votes
1 answer

How can I Aggregate and populate in mongoose

I'm having problems on populating the "Main" collection, grouping works pretty well but I really don't know how to populate or even .find after aggregating. I believe I'm doing a Model casting here or so: Main.aggregate([ {$match : query}, …
Michael
  • 187
  • 1
  • 13
3
votes
2 answers

Add to an object to Population in a Mongoose Model

I am new to Mongoose and getting my head around populations. I have Schema that looks like... var ProjectSchema = new Schema({ name: { type: String, default: '', required: 'Please fill Project name', trim: true …
Mike M
  • 4,879
  • 5
  • 38
  • 58
3
votes
4 answers

populate in post hook middlewhere for 'find' in mongoose

I have an article schema for articles posted on my site by users. It references the Users collection: var ArticleSchema = new Schema({ title: { // NO MARKDOWN FOR THIS, just straight up text for separating from content type: String, …
PGT
  • 1,468
  • 20
  • 34
3
votes
2 answers

Populate Multiple Mongoose Model Fields using Mongoose-paginate plugin

I have a Mongoose Schema (Appointment) with two ref fields (sender and receiver). Both referencing the same Schema (User). I am trying to populate these two fields using mongoose-paginate plugin. The Appointment Schema snippet. var paginate =…
xdzc
  • 1,421
  • 1
  • 17
  • 23
3
votes
0 answers

Mongoose Population: CastError: Cast to ObjectId failed for value "[object Object]" at path "_id"

Running into a CastError in Mongoose when populating nested ObjectId references (value {}) that are apparently valid, to the extent that they're not blocked when saving to schema. Interested in resolving this on the server-side to prevent malformed…
Yann Eves
  • 337
  • 1
  • 10
3
votes
0 answers

How to structure for multi-level nesting in mongodb using mongoose

Being and sql database user for many years I am having trouble working out how best to structure my data in mongodb using mongoose. As a practice project I am putting together a basic cms and I am stuck on how best to structure my page schema in…
Finglish
  • 9,692
  • 14
  • 70
  • 114
3
votes
1 answer

Mongoose.js conditional populate

I'm working with some old data where some of the schema has a "mixed" type. Basically sometimes a value will be a referenced ObjectID, but other times it'll be some text (super poor design). I unable to correctly populate this data because of the…
joseym
  • 1,332
  • 4
  • 20
  • 34
2
votes
2 answers

Mongoose .populate() is not working with node.js app

first post here, so hello to everyone! I'm having trouble with mongoose populate because it's apparently doing nothing. I'm going straight to the code. Here are my models: Space const spaceSchema = Schema( { user: { type:…
JOlle11
  • 21
  • 3
2
votes
2 answers

Flatten the nested Object in the Array of objects using Mongoose Populate

My query const users = usersWorkspaceModel .find({ workspaceId, userRole: 'supervisor', }) .select({ _id: 0, createdAt: 0, assignedBy: 0, updatedAt: 0, workspaceId: 0, }) .populate({ …
2
votes
1 answer

Why mongoose populate works but virtual populate doesn't?

I have the following schema as per the docs: const toolRecordSchema = new mongoose.Schema({ userId: { type: mongoose.Schema.Types.ObjectId }, }); toolRecordSchema.virtual("user", { ref: "User", localField: "userId", foreignField: "_id", …
2
votes
1 answer

Mongoose and MongoDB, how to create relation betwean two models with multiple references?

everyone. So I have this "blog" app where users can create posts with images. How my app works is that it loads different posts by userID. So I have a relation bewtean user and post by the user and post _id, however I also want to save username into…
2
votes
0 answers

Running aggregate query on mongodb using mongoose hook during find

I have a model that i want to get the user aggregate after some lookup during the login which I am thinking of adding to the mongoose hook below schema.pre("find", async function (next) { this.aggregate([ {$lookup: {from: "categories", localField:…
Nazehs
  • 478
  • 1
  • 10
  • 19
2
votes
1 answer

Mongoose populate() returns the '_id' field of populated documents with "new ObjectId()"

So I'm trying to fetch a set of company member documents that correspond to a specific user, and populate the company field of each document in the process. The aim here is to get the _id and the name fields of each company document that corresponds…
Nikitas IO
  • 696
  • 5
  • 20
2
votes
0 answers

How to update schema data in mongodb with mongoose and express

So I have a problem trying to work with mongodb. Firstly, I created a mongoose model like below which stores name. const mongoose = require("mongoose"); const schema = new mongoose.Schema({ name: { type: String }, }); …
FatFatty
  • 76
  • 1
  • 13