Specific element update in a Mongoose array

0

How to update the following registry:  

  "_id": "56dd4489800f800000000000",
  "registro": "2014-09-07T09:06:17.214Z",
  "stars": 0,
  "observacoes": [
    {
      "texto": "",
      "dataResposta": "",
"idUsuario": "56dc9f9f68d7078888888888", "id":2211 }, { "texto": "", "dataResposta": "",
"idUsuario": "56dc9f9f68d707888877778", "id":2212 } ]
How to update an element of array observations using mongoose? I tested some solutions of the type that follow, but to no avail.

    Registro.update(
     {_id : req.body._id, "observacoes.id":2212}, 
     { $inc : {stars : 1},
       $set:{texto:"Novo texto", dataResposta: new Date()}}, 
     {multi:false}, 
     function(error, regAfetados){...});
  
    
asked by anonymous 07.03.2016 / 10:27

1 answer

1

Actually this remarks should not be an array but rather another mongo document. Something like this:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const _UsersSchema = {
  nome_usuario: String
, telefone_usuario: String
}
const Users = new Schema(_UsersSchema);

const _Schema = {
  nome: {
    type: String
  , required: true
  , index: true
  }
  , id_usuario: Schema.Types.ObjectId
  , usuarios: [Users]
}

You can check the documentation to understand it better ...

    
15.07.2016 / 01:45