0

I am trying to update nested objects with Mongoose. But when I receive the request, it looks like this:

{
'example[apple]': 'false',
'example[pear]': 'false',
'example[banana]': 'false',
'example[orange]': 'false',
}

My model looks like this:

email: {
    type: String,
    index:true,
    unique: true,
},
example: {
  apple: {type:Boolean, default: true},
  banana: {type:Boolean, default: true},
  pear: {type:Boolean, default: true},
  orange: {type:Boolean, default: true}
}

And the object I am sending looks like this:

var formData = {
  example: {
    apple: false,
    banana: false,
    pear: false,
    orange: false
  }
}

What am I doing wrong?

2
  • who is generating and posting the request ? is it you. The structure of your request and the mongoose model are not aligned with each other. Commented Nov 2, 2016 at 3:16
  • @satishchennupati I am generating and posting the request. How should the model be to match? Commented Nov 2, 2016 at 4:00

1 Answer 1

1

First of all, the request body { 'example[apple]': 'false', 'example[pear]': 'false', 'example[banana]': 'false', 'example[orange]': 'false', } is a JSON object you can access: example.apple and so on.
exampleObject must be JSON object first. From your model it seems example is attribute in the model so to update you have to options:
1- Is to update all documents by not providing the id of the document:
yourmodel.findAndUpdate({no thing here},exampleObject,callBack)
2-Is to update by condition;preferred: yourmodel.findAndUpdate(condition,exampleObject,callBack)
condition can be = {_id:an id}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.