在mongoose中填充方法(节点js)

时间:2018-05-04 06:42:21

标签: javascript node.js mongodb mongoose mongoose-populate

我试图在节点js中使用populate()。 好吧,我试图访问一个集合的objectId到另一个集合。 例如,我有一个名为Project and events的集合, 我有这样的架构。

项目架构:

const projectSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    projectName: {
        type: String,
        required: true,
        unique: true
    },
    dimensions: {
        type: [],
        required: false
    },
    events: {
        [type: mongoose.Schema.Types.ObjectId],
        ref: 'EnrichedEvent'
    },
});
module.exports = mongoose.model('Project', projectSchema);

活动架构:

const enrichedEventSchema = mongoose.Schema({
    _id: mongoose.Schema.Types.ObjectId,
    projectId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Project',
        required: true
    },
    name: {
        type: String,
        required: true
    },
    type: {
        type: String,
        enum: ["Enriched"],
        required: true
    },
    source: {
        type: String,
        required: true
    },
});

以及项目的路由代码:

 const express = require("express");
 const router = express.Router();
 const mongoose = require("mongoose");
 const Project = require("../models/project");

 router.get("/", (req, res, next) => {
     Project.find()
         .populate('source', 'EnrichedEvents') //When i use this populate method, I m not able to GET any events on to browser..
         .exec()
         .then(docs => {
             const response = {
                 count: docs.length,
                 projects: docs.map(doc => {
                     return {
                         projectName: doc.projectName,
                         dimensions: doc.dimensions,
                         events: doc.events,
                         _id: doc._id
                     };
                 })
             };
             res.status(200).json(response);
         })
         .catch(err => {
             console.log(err);
             res.status(500).json({
                 error: err
             });
         });
 });

 router.post("/", (req, res, next) => {
     const project = new Project({
         _id: new mongoose.Types.ObjectId(),
         projectName: req.body.projectName,
         events: req.body.events
     });
     project
         .save()
         .then(result => {
             console.log(result);
             res.status(201).json({
                 message: "Created project successfully",
                 createdProject: {
                     projectName: result.projectName,
                     _id: result._id,
                     events: result.events,
                 }
             });
         })
         .catch(err => {
             console.log(err);
             res.status(500).json({
                 error: err
             });
         });
 });

 module.exports = router;

我的问题是我无法在项目页面中自动填充丰富的eventsId。 例如。每当我更新事件时,它们应该出现在项目页面中。但它没有发生。在事件页面中,我也没有得到相应的projectId。请指出我正确的方向。

1 个答案:

答案 0 :(得分:1)

Populate方法需要FIELD_TO_POPULATEFIELDS_TO_RETURN_FROM_POPULATED_DOC,而您正以相反的方式传递它。同样在您的项目架构中,您已使用events EnrichedEvent,因此在填充时使用events而不是EnrichedEvent;

尝试以下方法:

来自:

.populate('source', 'EnrichedEvents')

TO:

.populate('events', 'EnrichedEvent')

编辑:

更新EnrichedEvent的架构:

events: [{
 mongoose.Schema.Types.ObjectId, 
 ref: 'EnrichedEvent'
}]

现在应该可以了。