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 );`
5
  • Something like this? Commented Sep 20, 2016 at 13:49
  • yes in effect I'm trying this solution aggiungiGiorno: function(id,idC,doc,){ Clienti.update({ _id: id, "cantieri._id": idC },{ $push: { "cantieri.giorni": doc} }); } but the console log not return nothing and the document it'snt updated! I don't know how fix it? Commented Sep 20, 2016 at 15:00
  • forum meteor Commented Sep 20, 2016 at 15:12
  • You forgot the $ in the field name. Commented Sep 20, 2016 at 15:14
  • where? how field name? Commented Sep 20, 2016 at 15:17

1 Answer 1

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
    }
  });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Combining SimpleSchemas it's not the same thing?
Moreover new SimpleSchema is not an object?
I have updated my answer. To combine schemas, you should be able to keep your original code and use the query in my update.

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.