我正在尝试在节点js中使用populate()。
好吧,我正在尝试将一个集合的objectId访问到另一个集合。
例如,我有一个名为Project和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。请指出正确的方向。

最佳答案

填充方法采用FIELD_TO_POPULATEFIELDS_TO_RETURN_FROM_POPULATED_DOC,并且您以相反的方式传递它。同样在项目模式中,您将events用作EnrichedEvent,因此在填充时,请使用events而不是EnrichedEvent

请尝试以下操作:

从:

.populate('source', 'EnrichedEvents')

至:

.populate('events', 'EnrichedEvent')

编辑:

更新EnrichedEvent的架构:

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


现在应该可以工作了。

关于javascript - 在 Mongoose 中填充方法( Node js),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50168755/

10-13 00:21