操作器集在 mongodb 中不起作用。或者任何其他方法可以在文档中添加新字段?



运算符$set无法添加新字段hide。我想,我所做的一切都是按照mongodb的官方文档。有人能告诉我我做错了什么吗。感谢

createClass
.updateMany(
{ classname: req.body.className },
{ $set : {"hide":true}},
{ multi: true, upsert: false },
)

这是模式:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const Classes = new Schema({
classname: {
type: String,
required: true,
},
Subject: {
type: String,
required: true,
},
Chapter: {
type: String,
required: true,
},
Topic: {
type: String,
required: true,
},
SubjectimgPath: {
type: String,
},
ChapterimgPath: {
type: String,
},
TopicimgPath: {
type: String,
},
content: {
type: Array,
}
});
Classes.index({ "$**": "text" });
const Createclass = mongoose.model("AllClasses", Classes);
module.exports = Createclass;

试试这个:

const Classes = new Schema({
classname: {
type: String,
required: true,
},
Subject: {
type: String,
required: true,
},
Chapter: {
type: String,
required: true,
},
Topic: {
type: String,
required: true,
},
SubjectimgPath: {
type: String,
},
ChapterimgPath: {
type: String,
},
TopicimgPath: {
type: String,
},
content: {
type: Array,
}
}, {strict :false} );

最新更新