问题描述
我对 mongoose 和 nodejs 还很陌生,所以我在做我的项目时参考了 mongoose 文档。我想通过用 id 标识子文档来删除注释数组中的特定子文档。我尝试使用“pull”和“id”方法进行操作,如图所示。我的语法也没有发现任何错误,但它仍然有效。
这是来自我的数据库的示例文档:
{
comments: [
{
replyComment: [],_id: 601a673735644c83e0aa1be3,username: '[email protected]',email: '[email protected]',comment: 'test123'
},{
replyComment: [],_id: 601a6c94d1653c618c75ceae,comment: 'reply test'
},_id: 601eb7ba7233015d7090c6bf,comment: 'test comment'
},_id: 601ec090f5f22d75b41bec7b,comment: 'test comment123'
}
],_id: 601a3b8038b13e70405cf9ea,title: 'latest test',snippet: 'latest test snippet',body: 'latest test body',createdAt: 2021-02-03T05:58:24.123Z,updatedAt: 2021-02-07T06:56:53.902Z,__v: 15
}
通过做这个测试 findById("601a3b8038b13e70405cf9ea") 和 console.log(result)
我的topicSchema文件:
const mongoose = require('mongoose');
const Schema =mongoose.Schema;
const topicSchema = new Schema({
title: {
type: String,required: true
},snippet: {
type: String,body: {
type: String,comments: {
type: Array,required: false
}
},{timestamps: true},{ versionKey: false });
const Topic = mongoose.model('Topic',topicSchema);
module.exports = Topic;
我的评论架构文件:
const mongoose = require('mongoose');
const Schema =mongoose.Schema;
const comSchema = new Schema({
username: {
type: String,email: {
type: String,comment: {
type: String,replyComment: {
type: Array,required: false
},},{versionKey: false});
const Comm = mongoose.model('comm',comSchema);
module.exports = Comm;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)