0

I'm new in meteor and mongo I'd like to push one object in an array that is content in the other array. I'd like push giorni to cantieri. But I'd like push giorni in one specific cantieri. how can I make it? this my schema's collections.

`Clienti.Giorni = new SimpleSchema({
    giorno: {
        type: Date,
        label: "giorno del lavoro"
    },
    oraPartenza: {
        type: Date,
        label: 'Giorno e ora partenza',
    },
    oraInizio: {
        type: Date,
        label: 'Giorno e ora inizio',
        optional: true
    },
    oraFine: {
        type: Date,
        label: 'Giorno e ora fine',
        optional: true
    },
    dipendenti: {
        type: [Dipendenti]
    }
});

Clienti.Cantieri = new SimpleSchema({
    _id:{
        type: String,
        autoValue: function(){
            var id = new Meteor.Collection.ObjectID();
            return id._str
        }
    },
    nome: {
        type: String
    },
    luogo: {
        type: String
    },
    inizio: {
        type: Date
    },
    scadenza: {
        type: Date
    },
    inCorso: {
        type: Boolean,
        defaultValue: false
    },
    createdAt: {
        type: Date,
        label: "Creato il",
        autoValue: function() {
            return new Date()
        }
    },
        giorni: {
        type: [Clienti.Giorni],
        optional: true,
        autoform: {
            type: "hidden"
        }
    }
});

Clienti.ClienteSchema = new SimpleSchema({
    nome: {
        type: String,
        label: "nome"
    },
    iva: {
        type: String,
        label: "Partita iva",
        max: 16
    },
    referente: {
        type: String,
        label: "Nome persona di rifermento"
    },
    email: {
        type: String,
        label: "email"
    },
    indirizzo:{
        type:String,
        label: 'Indirizzo'
    },
    createdAt: {
        type: Date,
        label: "Creato il",
        autoValue: function() {
            return new Date()
        },
        autoform: {
            type: "hidden"
        }
    },
    cantieri: {
        type: [Clienti.Cantieri],
        optional: true,
        autoform: {
            type: "hidden"
        }
    }
});

Clienti.attachSchema( Clienti.ClienteSchema );`
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
Richard Ortiz
  • 29
  • 1
  • 1
  • 8

1 Answers1

1

I'm surprised you are not getting errors when trying to update your Clienti collection. According to the Simple Schema documentation in your schema definition, the type field should be a data type like String, Number, Boolean, Object or a constructor function like Date, and you can use any of these inside of square brackets to define it as an array of those data types (e.g., [String]).

So, one issue is that in your Clienti collection, you have defined your data type for cantieri as [Clienti.Cantieri]. This is not an acceptable data type. If I am understanding what you are trying to do correctly, you probably want the cantieri field definition in your Clienti collection to look like:

cantieri: {
    type: [Object],
    optional: true,
    autoform: {
        type: "hidden"
    }
}

And after this, you need to add each cantieri field under this item using the format:

cantieri.$.nome: {
    type: String
},
cantieri.$.luogo: {
    type: String
}

You also want to add the giorni fields under the cantieri fields in the Clienti collection in the same format:

giorni: {
    type: [Object],
    optional: true,
    autoform: {
        type: "hidden"
    }
},
giorni.$.giorno: {
    type: Date,
    label: "giorno del lavoro"
},
giorni.$.oraPartenza: {
    type: Date,
    label: 'Giorno e ora partenza',
}

Then, your method to update the database would look something like:

aggiungiGiorno: function(id, idC, doc,) { 
  Clienti.update({ 
    _id: id, 
    "cantieri._id": idC 
  }, { 
    $push: { 
      "cantieri": doc
    }
  });
}

UPDATE:

If you want to combine your schemas as above, you should be able to also update the document using the query:

aggiungiGiorno: function(id, idC, doc,) { 
  Clienti.update({ 
    _id: id, 
    "cantieri._id": idC 
  }, { 
    $push: { 
      "cantieri.$.giorni": doc
    }
  });
}
NFab
  • 386
  • 4
  • 6