Mongoose Populate not working

Hello, I have this circuit (called schema.js):

var mongoose = require('mongoose'), Schema = mongoose.Schema; var RoomSchema = new Schema({ name: { type: String, required: true, index: { unique: true } }, people: { type: Number, required: true }, childrens: {type: Number, required: true}, total: {type: Number, required: true} }); var Room = mongoose.model('Room', RoomSchema); var AvSchema = new Schema({ roomId: {type: Schema.Types.ObjectId, ref: 'Room'}, people: { type: Number, required: true }, childrens: {type: Number, required: true}, total: {type: Number, required: true} }); var Av = mongoose.model('Av', AvSchema); module.exports = { Room: Room, Av: Av }; 

in my route file:

 module.exports = function(app) { var model = require('../models/Schema'); app.get('/api/rooms', function(req, res) { model.Room.find(function(err, rooms) { if (err) res.send(err); res.json(rooms); }); }); app.get('/api/av', function(req, res) { model.Av.find().populate('roomId').exec(function(err, av) { if (err) res.send(err); res.json(av); }); }); }; 

Figure db: enter image description here

GET / api / rooms - answer:

 [{ "_id": "5444d0dd9a31437167eea816", "name": "Single", "people": 1, "childrens": 1, "total": 4 }, { "_id": "5444d1009a31437167eea817", "name": "Double", "people": 2, "childrens": 2, "total": 10 }] 

When I call api / rooms it looks fine, but when I call api / av I got an empty array [] .... Any idea what I am doing wrong? I must mention that I inserted the entries in the av collection for both rooms ID

Thanks in advance.

+11
source share
5 answers

By default, Mongoose pluralizes the model name to create a collection name, which is why Mongoose looks for avs in the collection instead of av .

You can explicitly set the name of the collection by passing this as the third parameter in model :

 var Av = mongoose.model('Av', AvSchema, 'av'); 
+14
source

As in CodyBugstein's answer, I write why this did not work in my case, although this is not the same case as the OP.

I tried to populate the "pro" field of my circuit with .post('save') as follows:

 mySchema.post('save', function(doc, next) { console.log(doc.pro); // Expected to log ObjectID doc.populate("pro"); // Populate field console.log(doc.pro); // Expected to log actual pro document } 

However , the 2nd console.log also registered an ObjectID instead of a document.

After working with this for an entire hour and trying different approaches, I found that all I had to do was use the promises and call execPopulate() so that it returned a full promise. I used async/await but you can also use .then :

 mySchema.post('save', async function(doc, next) { console.log(doc.pro); // Expected to log ObjectID await doc.populate("pro").execPopulate(); // Populate field console.log(doc.pro); // Expected to log actual pro document } 

So the 2nd console.log really logged the entire document, as expected :)

+4
source

Since this is the most popular result for a query

mongoose populate does not work

I will tell you why it did not work for me, although this is not a direct answer to this already resolved question, in the hope that this will help someone

The problem for me was that I specified the fields in select({..} , but not in the field that I was trying to fill.

+2
source

Remember to add the ref property to the schema for the property you are trying to populate. for instance

 // ... const orderSchema = new mongoose.Schema({ userId: { type: Types.ObjectId, required: true }, reservationId: { type: Types.ObjectId, required: true, ref: 'Reservation' // <-- don't forget the ref } }, { timestamps: true }) // ... 

See Mongoose Population

0
source

For me, the problem was that I did not need to fill out the model at the beginning of the file.

-2
source

Source: https://habr.com/ru/post/977126/


All Articles