I currently have a schema which currently 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 two users, each with two embedded position documents.
user1:
"position" : [
{
"title" : "Web Developer",
"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",
"_id" : ObjectId("58d6af99e4318f4703cebsju7")
},
{
"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",
"_id" : ObjectId("58d6af99e4318f4703ceb6aj")
}
],
My Query looks like:
app.post('/search', function (req, res) {
var position = new RegExp(req.body.position, 'i');
User.find({'position.title': position}, 'position.$').exec(function (err, result) {
console.log(result);
res.send({ results: result });
}); //
});
When searching 'Web Developer', this will return the first 'Web Developer' entry within the first user, and the 'Web Developer' entry in the second user, but I cant seem to return any subsequent entries and I am just wondering is it a MongoDB issue that I can only return one subdocument that matches per user? Or is there something thats wrong in my code?
The returned object can be seen below, the results object returns the two users, each with one position object, however there should be 3.
Suppose I have an array within the positions array, how would I access this?
my current code:
app.get('/applied', function(req, res){
User.aggregate(
{$unwind : "$position"},
{$unwind : "$position.applied"},
{$match:{'position.applied.candidate_id': "58dc2bd4e7208a3ea143959e"}}).exec(function (err, result) {
console.log(result);
});
res.render('applied', { title: 'applied'});
});
My Schema:
position: [{
title: String,
location: String,
start: String,
term:Number,
description:String,
date: {type: Date, default: Date.now},
applied:[{
candidate_id: String,
name: String
}],
}],
You can use $unwind for this it will fetch you multiple entries from collection unlike element match or '.' operator which return only first match data
User.aggregate(
{$unwind : "$position"},
{$match:{'position.title': position}}).exec(function (err, result) {
console.log(result);
res.send({ results: result });
});