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

Find documents by populate match result in mongoose

For Ecample, From this data with mongoose : Data Students : [{ "_id": ObjectId("5afbb519a7fe344ff8db67e6"), "name":"Jack", "age":20 ... },{ "_id": ObjectId("5afbb534a7fe344gf7db64e7"), "name":"Joni", "age":20 ... }] Data Activities…
2
votes
1 answer

how to reference a nested model in another model, mongoose 5.0

How to reference a nested model in another model. For example, having this shema, where I want to reference subcategories in User schema: var UserSchema = Schema({ publications: [{ name:String, categories: [{ type:…
santiago
  • 33
  • 1
  • 8
2
votes
2 answers

TypeScript with Mongoose: cannot read property 'CasterConstructor' of undefined

I am writing a node.js application using TypeScript and Mongoose. I am using the TypeScript model approach and continue to receive "cannot read property CasterConstructor' of undefined" when I call the create method to create a new subdocument. An…
user1790300
  • 2,143
  • 10
  • 54
  • 123
2
votes
2 answers

ModeMongoose's Async/Await with Mongoose Deep Populate Chain

I'm having a lot of trouble getting the chained mongoose commands run in sequential order when using async/await. A simple await Model.find({}) command works as one might expect in an asynchronous function, but when I chain find commands with lean,…
Trevor
  • 1,284
  • 3
  • 15
  • 33
2
votes
0 answers

Mongoose virtual, multiple localfields w/ multiple foreignFields

letterSchema.virtual('vObj_department', { ref: 'Department', localField: 'LOC_ID LET_DEP_ID', foreignField: 'LOC_ID DEP_ID', justOne: true, }) Both the Letter and the Department schemas have a LOC_ID variable. I wish to join these…
2
votes
0 answers

Mongoose: Designing a Relational Schema Properly

I am trying to set up some relational data between a few objects in my mongodb/monogoose database using references (storing ID in properties.) I have written what I have implemented and my shortcomings. This is an expansive post, please see my TL;DR…
Moshe
  • 2,583
  • 4
  • 27
  • 70
2
votes
0 answers

Mongoose populate array of object ids

I'm trying to populate my Thread schemas GET response with all the comments related that that specific thread. I've specified a path within the Thread model to accept an array of Comments that I'll then populate when I request a thread, but it's…
Jordan
  • 157
  • 5
  • 17
2
votes
0 answers

Mongoose: Multiple 3 Level Deep Population

I have the following schemas: var userSchema = new Schema({ username: String, rooms: [{type: Schema.Types.ObjectId, ref: 'Room'}] }); var roomSchema = new Schema({ members: [ { …
2
votes
1 answer

Mongoose query: populate top 2 comments from Post Schema

I have 3 collections: User, Post and Comment. Posts has multiple comments. I want grab 50 posts, populate author, populate comments but I want only top 2 most voted comments sorted by date(_id) const PostSchema = new Schema({ author: { type:…
Artur
  • 347
  • 1
  • 2
  • 11
2
votes
1 answer

how to find all documents with only looking at first letter of the values in mongoose

i want to find all documents by its first letter. the case is when i query in mysql then i could do WHERE Stores LIKE 'a%' the result would be all data with the first letter a. the question are : How to make the same query using mongoose? how…
2
votes
3 answers

Mongoose: schema._getVirtual is not a function

I'm having trouble with population all of a sudden (was working fine before I updated Mongoose package version). Currently using Mongoose 4.7.6. var userSchema = require('schemas/user'), User = db.model('User', userSchema); // db is already…
2
votes
0 answers

Mongoose and Node JS - improve find

I have collection A and collection B. I need to get all B (distinct preferably) that are in A that have a simple elemMatch. In this case A is Formation and B is Course. I only need the fields that are being currently fetched and nothing else. At the…
Duarte Mendes
  • 161
  • 3
  • 12
2
votes
1 answer

Mongoose: Overwriting array property on update

How can I overwrite an array property of a document based on a given array as the source? Schema: var postSchema = new mongoose.Schema({ title: { type: String, required: true, index: { unique: true } }, content: { type: String }, tags:…
2
votes
0 answers

Find all documents where subdocument is not null

I have two models, I shorten them for sake of simplicity. I want to get all videos where user is not null. var Video = new mongoose.Schema({ user : {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, status : {type: String,…
tadija
  • 23
  • 3
  • 6
2
votes
1 answer

Mongoose Populate not working with Array of ObjectIds

Here is my schema: /** Schemas */ var profile = Schema({ EmailAddress: String, FirstName: String, LastName: String, BusinessName: String }); var convSchema = Schema({ name: String, users: [{ type:…
Shreejibawa
  • 1,860
  • 1
  • 25
  • 35