2016-09-19 7 views
0

Ich bin neu in Meteor und Mongo Ich möchte ein Objekt in einem Array, das Inhalt in dem anderen Array ist, schieben. Ich möchte Push giorni zu Cantieri. Aber ich möchte Push Giorni in einer bestimmten Cantieri. Wie kann ich es schaffen? Dies sind die Sammlungen meines Schemas.Objekt in Array mit Meteor 1.4 schieben?

`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);` 
+0

Etwas wie [dies] (http://stackoverflow.com/questions/27874469/mongodb-push-in-nested-array)? – MasterAM

+0

ja in der Tat, ich versuche diese Lösung 'aggiungiGiorno: Funktion (ID, IDC, doc,) { \t \t Clienti.update ({_id: id," Cantieri._id ": IDC}, {$ push: { \t \t \t "cantieri.giorni": doc} \t \t}); \t} ' aber das Konsolenprotokoll gibt nichts zurück und das Dokument wird nicht aktualisiert! Ich weiß nicht, wie repariere ich das? –

+0

[Forum Meteor] (https://forums.meteor.com/t/push-object-in-array-help-me-please/29493/3) –

Antwort

0

Ich bin überrascht, dass Sie keine Fehler erhalten, wenn Sie versuchen, Ihre Clienti Sammlung zu aktualisieren. Nach den Simple Schema documentation in Ihrem Schema Definition sollte das type Feld ein Datentyp wie String, Number, Boolean, Object oder eine Konstruktorfunktion wie Date sein, und Sie können innerhalb von eckigen Klammern jeden dieser verwenden Sie es als ein Array von definieren diese Datentypen (z. B. [String]).

Ein Problem ist also, dass Sie in Ihrer Clienti Sammlung Ihren Datentyp für cantieri als [Clienti.Cantieri] definiert haben. Dies ist kein akzeptabler Datentyp. Wenn ich verstehe, was Sie versuchen richtig zu tun, möchten Sie wahrscheinlich die cantieri Felddefinition in Ihrer Clienti Sammlung wie folgt aussehen:

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

Und danach müssen Sie jedes cantieri Feld unter diesem Titel hinzufügen die Verwendung von Format:

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

Sie wollen auch die giorni Felder unter den cantieri Felder in der Clienti Sammlung im gleichen Format hinzufügen:

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', 
} 

Dann Ihre Methode, um die Datenbank zu aktualisieren würde in etwa so aussehen:

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

UPDATE:

Wenn Sie Ihre Schemata wie oben kombinieren möchten, sollten Sie in der Lage sein, auch um das Dokument zu aktualisieren, indem die Abfrage:

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

[SimpleSchemas kombinieren] (https://github.com/aldeed/meteo-simple-schema#combining-simpleschemas) ist es nicht das Gleiche? –

+0

Außerdem ist das neue SimpleSchema kein Objekt? –

+0

Ich habe meine Antwort aktualisiert. Um Schemas zu kombinieren, sollten Sie in der Lage sein, Ihren ursprünglichen Code zu behalten und die Abfrage in meinem Update zu verwenden. – NFab

Verwandte Themen