mongoose-populate

Mongoose populating array of subdocuments

梦想与她 提交于 2019-12-12 13:19:30
问题 Apologies if this has already been asked, my searches did not turn up the same situation. I have two schemas something like the below: var experimentSchema = new mongoose.Schema({ name : 'string' elements : [{ type : mongoose.Schema.ObjectId, ref: 'Element' }], resources : [{ type : mongoose.Schema.ObjectId, ref : 'Resource' }], }) var elementSchema = new mongoose.Schema({ name : 'string', component : { type : mongoose.Schema.ObjectId, ref : 'Component' } }) I want to perform a deep

deep populating in mongoose

痴心易碎 提交于 2019-12-12 05:58:28
问题 I've got two schemas, one for user and another one for post in the user schema, I've got a property for latestPost which would be an ObjectId of an entry in the post schema when I load up the user object, I want to get the lastestPost as an object that includes the author's username from the user schema where the author is an ObjectId that'd match an _id field in the user schema. the mongoose tutorials seem to use the syntax of User.findOne({ _id: req.user.id}) .populate('latestPost')

Mongoose populate subdoc

孤者浪人 提交于 2019-12-12 04:29:11
问题 I have an orderSchema that has a subdoc productOrder : const paymentTrans = require('./PaymentTrans').schema; const productOrder = require('./ProductOrder').schema; const orderSchema = new mongoose.Schema({ orderId: { type: mongoose.Schema.Types.ObjectId }, userId : {type: mongoose.Schema.Types.ObjectId, ref: 'User'}, productOrder: [productOrder], totalPerOrder: {type: Number, default:''}, paymentTrans: [paymentTrans] }, { timestamps: true }); const Order = mongoose.model('Order', orderSchema

How to populate through array of objects in mongoose

非 Y 不嫁゛ 提交于 2019-12-12 03:54:15
问题 I have a data like follows "student" : [ ObjectId("58500ea5ef914125073b040f"), ObjectId("58500ea5ef914125073b042e") ], my model, student: [{type: Schema.ObjectId,ref: 'Student'}], I want to populate student in these array, Classroom.findById(id).populate('student.student'){} It is not working,can anyone please suggest help.Thanks. 回答1: From what i can see in your data, student is the array of students, so you need to just write student in the populate query. this should work for you :

Trying to understand the use of the populate method

喜你入骨 提交于 2019-12-12 02:39:37
问题 I see that one way we use populate is to put one document from another collection into a "parent" collection. I was just going through this question and I was hoping someone could explain the answer to me better. And show me a practical use. Here is an example from the answer. var PersonSchema = new mongoose.Schema({ t: String }, {collection: 'persons'}); var User = mongoose.model('User', PersonSchema.extend({ _id: String, name: String })); var ParentSchema = new mongoose.Schema({ s: String }

Mongoose nested reference population

微笑、不失礼 提交于 2019-12-12 01:52:59
问题 I'm having trouble understanding some of the concepts behind mongoose's populate methods. I had an embedded approach working first, although, as I feared a big overhead of data and out-of-sync documents going around I tried changing the paradigm to ref other documents. My Schema is similar to the following (removed irrelevant properties): var userSchema = mongoose.Schema({ name: {type: String, default:''}, favorites:{ users:[{type: Schema.Types.ObjectId, ref: this}], places:[{type: Schema

mongoose populate shows schema hasn't been registered for model populate

↘锁芯ラ 提交于 2019-12-11 17:53:02
问题 created mongoose schema called movie with property of object reference to another collection of created by multer-gridfs storage but when called populate method i got null on fileID The schema is const mongoose = require('mongoose'); const Schema = mongoose.Schema; const MovieSchema = new Schema({ description: String, category: String, token: String, fileID: { type: mongoose.Schema.Types.ObjectId, ref: 'fs' //created by multer gridfs storage } }); const Movie = mongoose.model('Movies',

How to restrict deep population in mongoose-autopopulate

北城以北 提交于 2019-12-11 17:18:04
问题 I am using mongoose-autopopulate npm package. In one query I want to deep populate and in another query I want to populate till one level. Here's my schema which is deep populating the data. ChildSchema = new Schema({ plugin_id: { type: String }, title: { type: String }, //card title text: { type: String }, is_valid: { type: Boolean,require:true, default: false}, children: [{ type: Schema.Types.ObjectId, ref: 'Child', autopopulate: true }] }); ChildSchema.plugin(autopopulate); module.exports

How to access mongoose virtuals in express

会有一股神秘感。 提交于 2019-12-11 06:17:31
问题 I am trying to build a many to many relationship schema and trying to build a virtual to access the same. Finally trying to get the embedded objects when queried. My schema looks something like below //Mongoose Schema var item1 = new Schema ({ _id: Number, Item2Id: [{type: Number, ref: item2}], Detail1: String, Detail2: String ... },{toObject:{virtuals:true},toJSON:{virtuals:true}}); var item2 = new Schema ({ _id: Number, Item1Id: [{type: Number, ref: item1}], Detail1: String, Detail2: String

sort in populate does not work (Mongoose)

巧了我就是萌 提交于 2019-12-11 05:07:39
问题 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: mongoose.Schema.Types.ObjectId, ref: 'Message' } }); export const ChatModel = mongoose.model('Chat', chatSchema); // message const messageSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, chat: { type: mongoose.Schema