1
在Node js mongoose中需要搜索以下模式級別的文章描述。貓鼬怎麼可能呢? 我曾嘗試使用$ elemMatch和它不工作。模式級別如下。節點js mongoose通過深層嵌套文檔查找
var articleSchema = new Schema({
name: { type: String, required: true },
displayName: { type: String },
description: { type: String },
});
mongoose.model('Article', articleSchema);
var subChapterSchema = new Schema({
name: {type: String, required: true},
displayName: {type: String},
Articles:[articleSchema],
});
mongoose.model('SubChapter', subChapterSchema);
var chapterSchema = new Schema({
name: {type: String, required: true },
displayName: {type: String },
subChapters: [subChapterSchema],
});
mongoose.model('Chapter', chapterSchema);
var agreementSchema = new Schema({
name: {type: String, required: true },
displayName: {type: String },
Chapters: [chapterSchema],
});
mongoose.model('Agreement', agreementSchema);
我已經嘗試如下,但它不工作。
var regex = new RegExp(text, "i");
var criteria = {
Chapters.subChapters.Articles : {
$elemMatch: {
description:regex
}
}
}
Agreement.find({criteria},'name displayName',function(err,docs){
if (err)
console.log('error occured in the database');
console.log(docs);
});
'子章節時,你應該使用
""
:[chapterSchema]'或'章節:[chapterSchema]''中'agreementSchema? –章節:[chapterSchema] –
記住'key'區分大小寫'章節'和'章節'是不同的 –