node.js - node js mongoose find by deeply nested documents -


in node js mongoose need search article description in following schema levels. how can possible mongoose. have tried using $elemmatch , not working. schema level follows.

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); 

i have tried follows.but not working.

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 database');         console.log(docs);     });  

you can try $regex , $options.

when criteria object no need use {criteria} in find use find(criteria. if subchapters:[chapterschema] in agreementschema use subchapters.subchapters.articles.description: in example used chapters.subchapters.articles.description:.

and should use "" when want find nested field

var text = 'search text';  var criteria = {   "chapters.subchapters.articles.description": { $regex: text, $options: 'i' } };  agreement.find(criteria, 'name displayname', function (err, docs) {   if (err)     console.log('error occured in database');   console.log(docs); });  

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -