mongoose - Find all subdocuments that match with node.js mongodb -


i have schema looks like:

var user = new schema({     id: string,     firstname: string,     lastname: string,     password: string,     username: string,     position: [{           title: string,           location: string,           start: string,           term:number,           description:string,                     date: {type: date, default: date.now}   }]  }); 

i have 2 users, each 2 embedded position documents.

user1:

"position" : [      {         "title" : "business analyst",         "location" : "dublin",         "start" : "may 2017",         "term" : 6,         "description" : " lorem ipsum dolor sit amet, consectetur adipiscing elit. vivamus quis erat vitae dsit amet, consectetur adipiscing elit. vivamus quis erat vitae dolor tempus euismod non in mi",         "_id" : objectid("58d6b7e11e793c9a506ffe0f")     },      {         "description" : "description",         "term" : 12,         "start" : "may 2018",         "location" : "dublin",         "title" : "web developer",         "_id" : objectid("58d6af99e4318f4703ceb2af")     } ], 

user2:

"position" : [      {         "title" : "software engineer",         "location" : "cork",         "start" : "may 2017",         "term" : 9,         "description" : " lorem ipsum dolor sit amet, consectetur adipiscing elit. vivamus quis erat vitae dolor tempus euismod non in miorem ipsum dolor sit amet, consectetur adipiscing elit. vivamus quis erat vitae dolor tempus euismod non in mi"      },      {         "title" : "web developer",         "location" : "waterford",         "start" : "may 2017",         "term" : 6,         "description" : " lorem ipsum dolor sit amet, consectetur adipiscing elit. vivamus quis erat vitae dsit amet, consectetur adipiscing elit. vivamus quis erat vitae dolor tempus euismod non in mi"     } ], 

however, if search 'web developer', can seem return first user (i understand why entire document returns) can search , of subdocuments match string 'web developer'? i'm using ajax , post query follows:

    app.post('/search', function (req, res) {    user.find({'position.title':req.body.position}).exec(function (err, result) {     res.send({ results: result }); }); }); 

returned object :

enter image description here

in order return sub document have pass field second parameter .find function.

var position = new regexp(req.body.position, 'i');  user.find({'position.title': position}, 'position.$') .exec(function (err, result) { }); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -