Before save to MongoDB some array fields , you can check from mongo db some validations.




       


CompanySchema.path("tv_broadcasters").validate(function(value, respond) {
  var self = this;
  this.constructor.find(
    {
      tv_broadcasters: {
        $in: value
      },
      _id: {
        $ne: self.id
      }
    },
    function(err, company_tvs) {
      if (err) throw err;
      company_tvs.forEach(function(tv, key) {
        console.log("tv_validate : ", tv);
        console.log("self.id :", self.id);
        console.log("tv.id :", tv._id);

        if (self.id === tv._id) return respond(true);
        return respond(false);
      });
      respond(true);
    }
  );
}, "The specified tv_broadcasters is already in use.");



https://gist.github.com/muratatak77/bbe743e379add276cf59cc1fc90b7efb

Comments

Popular posts from this blog

Design a notification system

NETFLIX HIGH-LEVEL SYSTEM DESIGN

URL Shortener System Design