// schema
const chapter = new Schema({
version: {
type: String,
index: true,
default: "kjv"
},
bookTitle: {
type: String,
index: true,
lowercase: true,
required: [true, "Book Title is required"]
},
chapterNo: {
type: String,
index: true,
lowercase: true,
required: [true, "Chapter Number is required"]
},
verses: [
{
type: String,
index: true,
text: true,
sparse: true,
unique: true,
required: [true, "Verses are required"]
}
]
});
// my documents are in this manner
books = [
{
bookTitle: "leviticus",
chapterNo: "chapter-1",
verses: [
'array of verses'
]
},
{
bookTitle: "leviticus",
chapterNo: "chapter-2",
verses: [
'array of verses'
]
}
]
// till the end
mongoose
.connect(
"mongodb://path|url/name",
{ useNewUrlParser: true, useCreateIndex: true }
)
.then(resp => console.log(`[Mongodb Server] started`))
.catch(err => console.log(err));
// @ts-ignore
mongoose.Promise = global.Promise;
Chapter.insertMany(books)
.then(docs => {mongoose.disconnect();
})
.catch(err => console.log(err));
// this is how i query
Chapter.find({
version: req.query.vrsn.toLowerCase(),
bookTitle: req.query.bk.toLowerCase(),
chapterNo: { $in: requests }
})
.select('-_id chapterNo bookTitle version verses')
.then((docs) => {
if (docs.length > 0) {
return res.status(200).json({
request: docs
});
}
res.status(422).json({
message: '...'
});
})
我有1000多个文档集合,这些文档使用相同的架构保存到db中。我可以检索除具有特定字段(bookTitle)值(leviticus)的文档以外的所有文档。具有此字段和值的所有文档都存在于数据库中,但是在查询时就好像它们不存在一样返回空。另外,在我的用户模式中,我也有一个字段(角色),我在保存时指定了它(最初),它工作正常,但突然开始消失。保存期间会提供模式中指定的所有字段,并且所有数据库中的显示都将显示,除了角色:'regular'|| “管理员”