使用meteor 1.4推送数组中的对象?

时间:2016-09-19 16:29:10

标签: node.js mongodb meteor

我是meteor和mongo的新手我想在另一个数组中的内容中推送一个对象。我想把giorni推到cantieri。但是我想把giorni推到一个特定的cantieri。我该怎么做?这是我的架构的集合。

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

1 个答案:

答案 0 :(得分:1)

我很惊讶您在尝试更新Clienti集合时没有收到错误。根据架构定义中的Simple Schema documentationtype字段应为StringNumberBooleanObject等数据类型或像Date这样的构造函数,您可以使用方括号内的任何一个来将其定义为这些数据类型的数组(例如,[String])。

因此,有一个问题是,在Clienti集合中,您已将cantieri的数据类型定义为[Clienti.Cantieri]。这不是可接受的数据类型。如果我了解您要正确执行的操作,您可能希望cantieri集合中的Clienti字段定义如下:

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

在此之后,您需要使用以下格式在此项目下添加每个cantieri字段:

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

您还希望以相同的格式在giorni集合的cantieri字段下添加Clienti字段:

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

然后,更新数据库的方法如下所示:

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

更新:

如果您想要如上所述组合您的模式,您应该能够使用查询更新文档:

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