0

I currently have a schema which 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" : "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 I search for 'Web Developer', I can only seem to return the first user (I understand why the entire document returns) but can I do a search and get all of the subdocuments that match the string 'Web Developer'? I'm using Ajax and my post query is as 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

user
  • 375
  • 2
  • 8
  • 22
  • Possible duplicate of [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – s7vr Mar 28 '17 at 15:32

1 Answers1

1

In order to return only the sub document you have to pass the field as a second parameter to the .find function.

var position = new RegExp(req.body.position, 'i');

User.find({'position.title': position}, 'position.$')
.exec(function (err, result) {
});
Kxng Kombian
  • 465
  • 4
  • 12
  • Thats great! Just a quick question, I've updated my question to show the object I'm returning, but how do I access the information? I was originally using data.results[i].position[i].title but the structure of this is a little bit different and I cant seem to get it working? also the 'data' part of it comes from the ajax call, the results is what im returning from the query. – user Mar 28 '17 at 10:13
  • I've updated the answer. In the selection of the field change to `'position.$'` – Kxng Kombian Mar 28 '17 at 10:22
  • I just tested the code and I have updated my database to have 2 'Web developer' entires for the first user and one entry for the second user, but I can only seem to get one of them from each user? – user Mar 28 '17 at 10:44