Julian Veling
Julian Veling

Reputation: 357

Mongo/Mongoose - markModified not working

I have the following query:

  Section.find({"clause": sub.section}, function(err, docs){
    var detail = parseInt(sub.detail);
    docs[0].subClauses[0].additionalDetails[detail-1].percentile = sub.percentile;
    docs[0].markModified("subClauses");
    docs[0].save(function(err, doc){
      if(err) { return callback(err) }; 
      callback();
    })
  })

When I log the doc inside the .save function it appears to have saved successfully. However on querying the db elsewhere the save has not happened. The object its trying to save to is 3 deep like this:

[
  {
  "clause": "1",
  "subClauses: 
   [
     {
     "clause":"1.1",
     "additionalDetails:
      [
        {
        "detail": "1",
        "content": "whatever" 
        }
      ]
    }
  ]  
 }
]

With several items inside each array.

Am I not able to save data to the nested array object using Mongo?

Upvotes: 0

Views: 2300

Answers (1)

Jpm61704
Jpm61704

Reputation: 60

If I understand what you are trying to do, then I think what you are wanting to use is the Mongo Position Operator ($). This is used to index nested arrays in a document. In your current code you are only referencing the first object in the array. Using the position operator you will be able to reference other positions based of some other data, such as a query.

Here is the documentation for the operator: http://docs.mongodb.org/manual/reference/operator/update/positional/

And here is another Stack Overflow response which may help a bit also: MongoDB $ operator

Upvotes: 1

Related Questions