最简短的回答是“是”和“否”。
确实有一种方法可以匹配各个数组元素并在单个语句中使用单独的值更新它们,因为您实际上可以提供“多个”arrayFilters
条件并在更新语句中使用这些标识符。
这里的特定示例的问题是“更改集”中的条目之一(最后一个条目)实际上与当前存在的任何数组成员都不匹配。这里的“假定”行动是$push将新的不匹配成员添加到未找到的数组中。然而那个特定的动作cannot完成在“单一操作”,但你可以使用bulkWrite()发布“多项”声明来涵盖该案件。
匹配不同的数组条件
以点为单位进行解释,请考虑“变更集”中的前两项。您可以申请一个"single"更新语句有多个arrayFilters
像这样:
db.avail_rates_copy.updateOne(
{ "_id": 12345 },
{
"$set": {
"rates.$[one]": {
"productId" : NumberInt(1234),
"rate" : 400.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201801)
},
"rates.$[two]": {
"productId" : NumberInt(1234),
"rate" : 500.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201802)
}
}
},
{
"arrayFilters": [
{
"one.productId": NumberInt(1234),
"one.rateCardId": NumberInt(1),
"one.month": NumberInt(201801)
},
{
"two.productId": NumberInt(1234),
"two.rateCardId": NumberInt(1),
"two.month": NumberInt(201802)
}
]
}
)
如果你运行它,你会看到修改后的文档变成:
{
"_id" : 12345,
"_class" : "com.example.ProductRates",
"rates" : [
{ // Matched and changed this by one
"productId" : 1234,
"rate" : 400,
"rateCardId" : 1,
"month" : 201801
},
{ // And this as two
"productId" : 1234,
"rate" : 500,
"rateCardId" : 1,
"month" : 201802
},
{
"productId" : 1234,
"rate" : 400,
"rateCardId" : 2,
"month" : 201803
},
{
"productId" : 1235,
"rate" : 500,
"rateCardId" : 1,
"month" : 201801
},
{
"productId" : 1235,
"rate" : 234,
"rateCardId" : 2,
"month" : 201803
}
]
}
请注意,您在列表中指定每个“标识符”arrayFilters
有多个条件来匹配元素,如下所示:
{
"one.productId": NumberInt(1234),
"one.rateCardId": NumberInt(1),
"one.month": NumberInt(201801)
},
因此,每个“条件”有效地映射为:
<identifier>.<property>
所以它知道要查看"rates"
数组由更新块中的语句组成$[<indentifier>] :
"rates.$[one]"
并查看每个元素"rates"
以符合条件。所以"one"
标识符将匹配前缀为的条件"one"
对于另一组前缀为的条件也是如此"two"
,因此实际的更新语句仅适用于那些与分配给标识符的条件匹配的语句。
如果你只是想要"rates"
属性而不是整个对象,那么您只需将其标记为:
{ "$set": { "rates.$[one].rate": 400, "rates.$[two].rate": 500 } }
添加不匹配的对象
所以第一部分相对容易理解,但正如所说的那样$push对于“不存在的元素”则是另一回事,因为我们基本上需要“文档”级别的查询条件才能确定数组元素“丢失”。
这本质上意味着您需要发布更新$push查找每个数组元素以查看它是否存在。当它不存在时,则该文档是匹配的并且$push被执行。
这是哪里bulkWrite()发挥作用,您可以通过为“更改集”中的每个元素向上面的第一个操作添加额外的更新来使用它:
db.avail_rates_copy.bulkWrite(
[
{ "updateOne": {
"filter": { "_id": 12345 },
"update": {
"$set": {
"rates.$[one]": {
"productId" : NumberInt(1234),
"rate" : 400.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201801)
},
"rates.$[two]": {
"productId" : NumberInt(1234),
"rate" : 500.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201802)
},
"rates.$[three]": {
"productId" : NumberInt(1235),
"rate" : 700.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201802)
}
}
},
"arrayFilters": [
{
"one.productId": NumberInt(1234),
"one.rateCardId": NumberInt(1),
"one.month": NumberInt(201801)
},
{
"two.productId": NumberInt(1234),
"two.rateCardId": NumberInt(1),
"two.month": NumberInt(201802)
},
{
"three.productId": NumberInt(1235),
"three.rateCardId": NumberInt(1),
"three.month": NumberInt(201802)
}
]
}},
{ "updateOne": {
"filter": {
"_id": 12345,
"rates": {
"$not": {
"$elemMatch": {
"productId" : NumberInt(1234),
"rateCardId": NumberInt(1),
"month" : NumberInt(201801)
}
}
}
},
"update": {
"$push": {
"rates": {
"productId" : NumberInt(1234),
"rate" : 400.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201801)
}
}
}
}},
{ "updateOne": {
"filter": {
"_id": 12345,
"rates": {
"$not": {
"$elemMatch": {
"productId" : NumberInt(1234),
"rateCardId": NumberInt(1),
"month" : NumberInt(201802)
}
}
}
},
"update": {
"$push": {
"rates": {
"productId" : NumberInt(1234),
"rate" : 500.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201802)
}
}
}
}},
{ "updateOne": {
"filter": {
"_id": 12345,
"rates": {
"$not": {
"$elemMatch": {
"productId" : NumberInt(1235),
"rateCardId": NumberInt(1),
"month" : NumberInt(201802)
}
}
}
},
"update": {
"$push": {
"rates": {
"productId" : NumberInt(1235),
"rate" : 700.0,
"rateCardId": NumberInt(1),
"month" : NumberInt(201802)
}
}
}
}}
],
{ "ordered": true }
)
请注意$elemMatch在查询过滤器中,因为这是通过“多个条件”匹配数组元素的要求。我们不需要那个arrayFilters
条目因为他们only查看它们已经应用到的每个数组项,但作为“查询”,条件需要$elemMatch因为简单的“点符号”会返回不正确的匹配。
另请参阅$not此处使用运算符来“否定”$elemMatch,因为我们的真实条件是只匹配一个文档“没有匹配的数组元素”符合所提供的条件,这就是选择附加新元素的理由。
向服务器发出的单个语句本质上是尝试four更新操作一个用于尝试更新匹配的数组元素,另一个用于每个three“改变集”试图$push
发现文档与“更改集”中数组元素的条件不匹配。
因此,结果正如预期的那样:
{
"_id" : 12345,
"_class" : "com.example.ProductRates",
"rates" : [
{ // matched and updated
"productId" : 1234,
"rate" : 400,
"rateCardId" : 1,
"month" : 201801
},
{ // matched and updated
"productId" : 1234,
"rate" : 500,
"rateCardId" : 1,
"month" : 201802
},
{
"productId" : 1234,
"rate" : 400,
"rateCardId" : 2,
"month" : 201803
},
{
"productId" : 1235,
"rate" : 500,
"rateCardId" : 1,
"month" : 201801
},
{
"productId" : 1235,
"rate" : 234,
"rateCardId" : 2,
"month" : 201803
},
{ // This was appended
"productId" : 1235,
"rate" : 700,
"rateCardId" : 1,
"month" : 201802
}
]
}
取决于实际不匹配的元素数量bulkWrite()
响应将报告其中有多少语句实际匹配并影响文档。在这种情况下是2
匹配和修改,因为“第一个”更新操作匹配现有数组条目,而“最后一个”更改更新匹配文档不包含数组条目并执行$push修改。
结论
所以你就有了组合方法,其中:
所以update,对于单个操作来说是“是”。但增加差异意味着多次操作。但您可以将这两种方法结合起来,正如此处演示的那样。
有许多“奇特”的方法可以使用代码根据“更改集”数组内容构造这些语句,因此您不需要对每个成员进行“硬编码”。
作为 JavaScript 的基本情况并与当前版本的 mongo shell 兼容(有点烦人的是它不支持对象扩展运算符):
db.getCollection('avail_rates_copy').drop();
db.getCollection('avail_rates_copy').insert(
{
"_id" : 12345,
"_class" : "com.example.ProductRates",
"rates" : [
{
"productId" : 1234,
"rate" : 100,
"rateCardId" : 1,
"month" : 201801
},
{
"productId" : 1234,
"rate" : 200,
"rateCardId" : 1,
"month" : 201802
},
{
"productId" : 1234,
"rate" : 400,
"rateCardId" : 2,
"month" : 201803
},
{
"productId" : 1235,
"rate" : 500,
"rateCardId" : 1,
"month" : 201801
},
{
"productId" : 1235,
"rate" : 234,
"rateCardId" : 2,
"month" : 201803
}
]
}
);
var changeSet = [
{
"productId" : 1234,
"rate" : 400.0,
"rateCardId": 1,
"month" : 201801
},
{
"productId" : 1234,
"rate" : 500.0,
"rateCardId": 1,
"month" : 201802
},
{
"productId" : 1235,
"rate" : 700.0,
"rateCardId": 1,
"month" : 201802
}
];
var arrayFilters = changeSet.map((obj,i) =>
Object.keys(obj).filter(k => k != 'rate' )
.reduce((o,k) => Object.assign(o, { [`u${i}.${k}`]: obj[k] }) ,{})
);
var $set = changeSet.reduce((o,r,i) =>
Object.assign(o, { [`rates.$[u${i}].rate`]: r.rate }), {});
var updates = [
{ "updateOne": {
"filter": { "_id": 12345 },
"update": { $set },
arrayFilters
}},
...changeSet.map(obj => (
{ "updateOne": {
"filter": {
"_id": 12345,
"rates": {
"$not": {
"$elemMatch": Object.keys(obj).filter(k => k != 'rate')
.reduce((o,k) => Object.assign(o, { [k]: obj[k] }),{})
}
}
},
"update": {
"$push": {
"rates": obj
}
}
}}
))
];
db.getCollection('avail_rates_copy').bulkWrite(updates,{ ordered: true });
这将动态构建“批量”更新操作的列表,如下所示:
[
{
"updateOne": {
"filter": {
"_id": 12345
},
"update": {
"$set": {
"rates.$[u0].rate": 400,
"rates.$[u1].rate": 500,
"rates.$[u2].rate": 700
}
},
"arrayFilters": [
{
"u0.productId": 1234,
"u0.rateCardId": 1,
"u0.month": 201801
},
{
"u1.productId": 1234,
"u1.rateCardId": 1,
"u1.month": 201802
},
{
"u2.productId": 1235,
"u2.rateCardId": 1,
"u2.month": 201802
}
]
}
},
{
"updateOne": {
"filter": {
"_id": 12345,
"rates": {
"$not": {
"$elemMatch": {
"productId": 1234,
"rateCardId": 1,
"month": 201801
}
}
}
},
"update": {
"$push": {
"rates": {
"productId": 1234,
"rate": 400,
"rateCardId": 1,
"month": 201801
}
}
}
}
},
{
"updateOne": {
"filter": {
"_id": 12345,
"rates": {
"$not": {
"$elemMatch": {
"productId": 1234,
"rateCardId": 1,
"month": 201802
}
}
}
},
"update": {
"$push": {
"rates": {
"productId": 1234,
"rate": 500,
"rateCardId": 1,
"month": 201802
}
}
}
}
},
{
"updateOne": {
"filter": {
"_id": 12345,
"rates": {
"$not": {
"$elemMatch": {
"productId": 1235,
"rateCardId": 1,
"month": 201802
}
}
}
},
"update": {
"$push": {
"rates": {
"productId": 1235,
"rate": 700,
"rateCardId": 1,
"month": 201802
}
}
}
}
}
]
就像一般答案的“长形式”中所描述的那样,但当然只是使用输入“数组”内容来构造所有这些语句。
您可以用任何语言进行此类动态对象构造,并且所有 MongoDB 驱动程序都接受您可以“操作”的某种结构类型的输入,然后在实际发送到服务器执行之前将其转换为 BSON。
NOTE : The <identifier>
for arrayFilters
must由字母数字字符组成并且must以字母字符开头。因此,在构造动态语句时,我们添加前缀"a"
然后是正在处理的每个项目的当前数组索引。