0
[
{
    "_id": "58b89de6a480ce48c8f3742d",
    "name": "Core Site",
    "version": "1.0.0",
    "author": "Jane Doe",
    "vendor": "Online Banking",
    "steps": [
        {
            "name": "Step 1: Fun Stuff",
            "dependencies": "1,2,3,4",
            "resource": "TS",
            "weight": 0
        },
        {
            "name": "Step 2: Weird Stuff",
            "dependencies": "3,4",
            "resource": "PS",
            "weight": 1
        }
    ]
},
{
    "_id": "58b8a22097fbe41746827ac7",
    "name": "Online Statements",
    "version": "1.0.0",
    "author": "John Doe",
    "vendor": "Online Banking",
    "steps": [
        {
            "name": "Step 1: Fun Stuff",
            "dependencies": "1,2,3,4",
            "resource": "TS",
            "weight": 0
        }
    ]
}]

I have saved this information into a MongoDB but when I try to get this information the sub-document "Steps" does not include the _id that I can see using Robomongo.

My schema is as follows:

    // grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var stepSchema = new Schema({
  name: { type: String, required: true},
  dependencies: String,
  resource: String,
  weight: Number
})

// create a schema
var sopSchema = new Schema({
  name: { type: String, required: true, unique: true },
  version: String,
  author: String,
  vendor: String,
  steps: [stepSchema]
});



// the schema is useless so far
// we need to create a model using it
var Sop = mongoose.model('Sop', sopSchema);

// make this available to our users in our Node applications
module.exports = Sop;

My end goal is to check if the step is already included and update it when making a change on the front end. So i wanted to get this reference to ensure i have the right subdocument.

My get statement is as follows in my nodejs server.

router.route('/sops')
    .get(function(req, res) {
        Sop.find(function(err, sops) {
            if (err)
                res.send(err);

                var subdoc = sops[0].steps[0];
                console.log(subdoc);
            res.json(sops);
    });
})
Aravind
  • 40,391
  • 16
  • 91
  • 110
  • In what way angular2 influences this post. ?? Removing it as it is not used here – Aravind Mar 18 '17 at 19:16
  • Thanks. I was consuming the rest endpoint with angular2 and wasn't capturing the _id but found it was wrong with the return itself. Sorry – Ben Gummelt Mar 18 '17 at 20:09
  • what is the problem that you are facing? – Aravind Mar 18 '17 at 20:10
  • I am trying to create a reactive form that can add multiple steps to a SOP. The main problem I am facing is how I can update a step if it is slightly modified without clearing out all the steps and just updating them as fresh steps. My first approach is to get the _id so I can pass that and do a For loop to determine what has changed. But if I can find what has changed in the form and just send that that would probably work too. – Ben Gummelt Mar 18 '17 at 20:20

2 Answers2

0

MongoDB will not add an _id to sub-documents for you. You need to add it manually.

So:

"steps": [
        {
            "_id": new ObjectId(),
            "name": "Step 1: Fun Stuff",
            "dependencies": "1,2,3,4",
            "resource": "TS",
            "weight": 0
        }
 ]
hya
  • 1,708
  • 2
  • 15
  • 22
  • When i use Robomongo i can see the _id in the db. Not sure why that is isn't coming thru. I can try to add the object just to see. – Ben Gummelt Mar 18 '17 at 22:41
0

Link to Article that Fixed the issue

  var stepSchema = new Schema({
  name: { type: String, required: true},
  dependencies: String,
  resource: String,
  weight: Number
},{ _id: true });

Added the _id: true attribute to the end to force an _id to be generated.

Community
  • 1
  • 1