流星更新 mongo
Meteor upsert with mongo
我需要使用 upsert,因为我的帐户模式(见下文)中的记录可能不存在。它存在的定义因素是 adviserID 和 period。如果存在与这两个匹配的记录,它应该只更新记录。
然而,当我 运行 这样做时,我收到此错误:已清理并向客户端报告为:顾问 ID 必须是一个对象 [400]。当我 console.log(adviser) 在 updateOrder 方法中时,它确实正确地报告了顾问 ObjectID。
如有任何帮助,我们将不胜感激。谢谢。
更新插入方法:
Meteor.methods({
updateOrder: function(adviser, order, period) {
Account.upsert(
{
adviserId: adviser,
period: period
}, {
$set: {
adviserId: adviser,
order: order,
outstanding: order,
period: period
}
});
}
});
架构:
Schema.Account = new SimpleSchema({
"adviserId": {
type: Object,
},
period: {
type: Date,
label: "Order Period"
},
order: {
type: Number,
min: 0,
label: "Order Number"
},
outstanding: {
type: Number,
min: 0,
defaultValue: 0,
label: "Outstanding Number"
}
});
您很可能希望 adviserId 是一个字符串。 MongoDB 内部 ID 对象是 Meteor 中的字符串。
这就是我们在 meteor 中插入的方式。
Shares.update({
adviserId: adviser,
period: period
}, {
$set: {
adviserId: adviser,
order: order,
outstanding: order,
period: period
}
}, {
upsert: true
})
来自 SimpleSchema 文档:
If you have a key with type Object, the properties of the object will be validated as well, so you must define all allowed properties in the schema.
adviser
在您的方法中应该是一个字符串,因为您将它用于查找(在 upsert 中)。如果您的 adviser
是一个字符串,那么只需修复 {type: String, regEx: SimpleSchema.RegEx.Id}
的架构。或者如果你想使用一个对象,那么你需要描述它或添加 blackbox option.
谢谢 Bryukhanov Valentin。转换为字符串类型解决了我的问题。
也感谢那些回复的人。
我需要使用 upsert,因为我的帐户模式(见下文)中的记录可能不存在。它存在的定义因素是 adviserID 和 period。如果存在与这两个匹配的记录,它应该只更新记录。
然而,当我 运行 这样做时,我收到此错误:已清理并向客户端报告为:顾问 ID 必须是一个对象 [400]。当我 console.log(adviser) 在 updateOrder 方法中时,它确实正确地报告了顾问 ObjectID。
如有任何帮助,我们将不胜感激。谢谢。
更新插入方法:
Meteor.methods({
updateOrder: function(adviser, order, period) {
Account.upsert(
{
adviserId: adviser,
period: period
}, {
$set: {
adviserId: adviser,
order: order,
outstanding: order,
period: period
}
});
}
});
架构:
Schema.Account = new SimpleSchema({
"adviserId": {
type: Object,
},
period: {
type: Date,
label: "Order Period"
},
order: {
type: Number,
min: 0,
label: "Order Number"
},
outstanding: {
type: Number,
min: 0,
defaultValue: 0,
label: "Outstanding Number"
}
});
您很可能希望 adviserId 是一个字符串。 MongoDB 内部 ID 对象是 Meteor 中的字符串。
这就是我们在 meteor 中插入的方式。
Shares.update({
adviserId: adviser,
period: period
}, {
$set: {
adviserId: adviser,
order: order,
outstanding: order,
period: period
}
}, {
upsert: true
})
来自 SimpleSchema 文档:
If you have a key with type Object, the properties of the object will be validated as well, so you must define all allowed properties in the schema.
adviser
在您的方法中应该是一个字符串,因为您将它用于查找(在 upsert 中)。如果您的 adviser
是一个字符串,那么只需修复 {type: String, regEx: SimpleSchema.RegEx.Id}
的架构。或者如果你想使用一个对象,那么你需要描述它或添加 blackbox option.
谢谢 Bryukhanov Valentin。转换为字符串类型解决了我的问题。
也感谢那些回复的人。