[英]populate method in mongoose (node js)
I m trying to use populate( ) in node js.我正在尝试在节点 js 中使用 populate()。 Well I m trying to access, objectId of one collection into another collection.好吧,我正在尝试将一个集合的 objectId 访问到另一个集合中。 for eg., i have collections called Project and events, where i have schema like this.例如,我有一个名为 Project 和 events 的集合,在那里我有这样的架构。
Project schema:项目架构:
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);
Events Schema:事件架构:
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
},
});
and the routing code for projects to :以及项目的路由代码:
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;
my problem is I can't auto populate the enriched eventsId
in projects page.我的问题是我无法在项目页面中自动填充丰富的eventsId
。 for eg.例如。 Whenever i update events they should appear in projects page.but its not happening.每当我更新事件时,它们应该出现在项目页面中。但它不会发生。 In events page also i am not getting corresponding projectId.在事件页面中,我也没有得到相应的 projectId。 please point me in right direction.请指出我正确的方向。
Populate method takes FIELD_TO_POPULATE
and FIELDS_TO_RETURN_FROM_POPULATED_DOC
and you are passing it the reversed way. 填充方法采用FIELD_TO_POPULATE
和FIELDS_TO_RETURN_FROM_POPULATED_DOC
并且您以相反的方式传递它。 Also in your project schema you have used events
for EnrichedEvent
, so while populating use events
not EnrichedEvent
; 同样在项目模式中,您已经为EnrichedEvent
使用了events
,因此在填充时使用的events
不是EnrichedEvent
。
Try the following: 请尝试以下操作:
From: 从:
.populate('source', 'EnrichedEvents')
TO: 至:
.populate('events', 'EnrichedEvent')
Edit: 编辑:
update your schema for EnrichedEvent
: 更新EnrichedEvent
的架构:
events: [{
mongoose.Schema.Types.ObjectId,
ref: 'EnrichedEvent'
}]
It should work now. 现在应该可以工作了。
I tried to use a similar .populate('...', '...')
method but unsuccessfully.我尝试使用类似的.populate('...', '...')
方法但没有成功。 I would use我会用
.populate('source').populate('EnrichedEvent')
or ('EnrichedEvents')
depending on what you have defined in your schema.或('EnrichedEvents')
取决于您在架构中定义的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.