Wednesday, 7 June 2017

findByIdAndUpdate using addToSet adding duplicate objectId's

I'm using mongoose findByIdAndUpdate to update my colection of projects, but when I use $addToSet the array isn't verified and a duplicate ObjectId is added to it.

I have two models, Users and Projects. Somes user may enter in a project and needs to be stored in 'time' field in Project Model.

My Model

var projetoSchema = mongoose.Schema({
    time : [{
        data_inclusao : {
            type : Date,
            default : Date.now
        },
        participante : {
            type : mongoose.Schema.Types.ObjectId,
            ref : 'Usuario'
        }
     }],
});

I tried to do with $ne but then i got the error:

CastError: Cast to ObjectId failed for value "{ '$ne' : 593600bf62bf771c01cb6464 }" at path "participante"

My controller

controller.entrarEmProjeto = function(req, res) {

let projetoId = req.body._id;
let criadorId = req.user._id;

Usuario.findById(criadorId)
  .exec()
  .then(function(usuario) {
    let dados = { 
      'time.participante' : { $ne : usuario.id },
      $addToSet : { time : usuario } };
    Projeto.findByIdAndUpdate(projetoId, dados)
      .exec()
      .then(function(projeto) {
        console.log(projeto);
        res.status(201).json(projeto);
      }, function(erro) {
        console.error(erro);
        res.status(500).json(erro);
      });
  });
};



via Samuel Monteiro

No comments:

Post a Comment