【发布时间】:2021-09-30 01:07:11
【问题描述】:
MongoDB 文档包含一个对象数组,如果我想从具有特定值的数组中查找和删除对象,查询这些文档的最佳方法是什么;
这是文档架构的示例
const mongoose = require("mongoose");
const LibrarySchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
books: [
{
type: new mongoose.Schema({
bookName: {
type: String,
required: true,
},
chapterReading: {
type: Number,
default: 1,
required: true,
},
}),
},
],
});
const Library = mongoose.model("Library", LibrarySchema);
exports.Library = Library;
如果我想查找并删除带有某个 bookName 的书
【问题讨论】:
-
要删除由其名称标识的特定书籍,您可以使用 update 操作/方法(CRUD 的 U)。此外,请参阅 MongoDB 手册。
标签: javascript node.js arrays mongodb mongoose