Mongoose 不在数组的 objectid 中填充 objectid
Posted
技术标签:
【中文标题】Mongoose 不在数组的 objectid 中填充 objectid【英文标题】:Mongoose do not populate objectid in an objectid of array 【发布时间】:2021-03-03 21:29:09 【问题描述】:这个问题有点长。所以我为你粗体输入了关键信息。
我开发了一个类似 *** 的项目。我有 4 个数据库:
问题 用户 解决方案 cmets我互相引用了这些架构。这是架构:
问题架构
const problemSchema = new mongoose.Schema(
title:
type: String,
required: [true, 'You have to enter a title']
,
content:
type: String,
required: [true, 'You have to enter a content']
,
createdAt:
type: Date,
default: Date.now()
,
slug:
type: String
,
solution: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Solution'
,
],
comment: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
],
votes:
type: Number,
default: 0
,
views:
type: Number,
default: 0
,
user:
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
)
module.exports = mongoose.model('Problem', problemSchema)
用户架构:
const userSchema = new mongoose.Schema(
name:
type: String,
required: true
,
email:
type: String,
required: [true, 'You have to enter an email'],
unique: true,
match: [
/^([\w-\.]+@([\w-]+\.)+[\w-]2,4)?$/,
'Please provide a valid email address.'
]
,
password:
type: String,
required: [true, 'You have to enter a password'],
minlength: [6, 'Your password cannot be less than 6 character.'],
select: false
,
role:
type: String,
default: 'user',
enum: ['user', 'admin']
,
createdAt:
type: Date,
default: Date.now()
,
about:
type: String
,
place:
type: String
,
age:
type: Number
,
blocked:
type: Boolean,
default: false
,
problem: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Problem'
,
],
solution: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Solution'
],
comment: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
]
)
和评论架构:
const commentSchema = new mongoose.Schema(
content:
type: String,
required: [true, 'You have to enter a content']
,
createdAt:
type: Date,
default: Date.now()
,
isFunctional:
type: Boolean,
default: false
,
user:
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
,
problem:
type: mongoose.Schema.Types.ObjectId,
ref: 'Problem'
,
)
module.exports = mongoose.model('Comment', commentSchema)
在我的项目中,我将问题发送到 MongoDB。然后我发送评论。保存 cmets 后,我将这些 cmets 添加到问题和用户数据库中。
cmet 保存在 DB 中的函数:
const Comment = require('../models/comment/Comment')
const Problem = require('../models/problem/Problem')
const User = require('../models/user/User')
const asyncErrorWrapper = require('express-async-handler')
const addCommentToProblem = asyncErrorWrapper(async (req, res, next) =>
const content, problemId = req.body
const newComment = await Comment.create(
content: content,
problem: problemId,
user: req.user.id,
)
const problemOfComment = await Problem.findByIdAndUpdate(problemId,
$push: comment: newComment._id
)
const userOfComment = await User.findByIdAndUpdate(req.user.id,
$push: comment: newComment._id
)
)
好吧,到目前为止一切都很好。问题来了。当我尝试解决问题时,我会填充一些字段,例如用户字段。所以我可以在这个问题的细节中添加用户信息。在问题模式中填充用户和评论时,它会向我发送数据。不过,我们还好。但是当我尝试在 cmets 中获取用户字段时,它不会填充用户。它只是用户信息的objectId。
这是我遇到问题的功能:
const getAProblem = asyncErrorWrapper(async (req, res, next) =>
const id = req.params
const problems = null
await Problem.findByIdAndUpdate(id,
$inc: views: 1
, new: true )
.populate('user') ==> THIS LINE WORKS
.populate('comment') ==> THIS LINE WORKS
.populate('comment.user') ==> THIS LINE DOES NOT WORK
.exec(function(err, post)
if(err)
console.log(err)
res
.status(200)
.json(
success: true,
data: post
)
);
)
感谢您的阅读和耐心。任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:请参阅https://mongoosejs.com/docs/populate.html 的文档
然后试试这个方法。
const getAProblem = asyncErrorWrapper(async (req, res, next) =>
const id = req.params
const problems = null
await Problem.findByIdAndUpdate(id,
$inc: views: 1
, new: true )
.populate('user') ==> THIS LINE WORKS
.populate(
'path': 'comment',
'populate':
'path':'user'
)
.exec(function(err, post)
if(err)
console.log(err)
res
.status(200)
.json(
success: true,
data: post
)
);
)
【讨论】:
以上是关于Mongoose 不在数组的 objectid 中填充 objectid的主要内容,如果未能解决你的问题,请参考以下文章
一个 ObjectId 的 ObjectId 的 Mongoose 查询数组
ObjectId 的 NodeJS/Mongoose 模式数组
Mongoose 删除(拉)数组中的文档,不适用于 ObjectID