gpt4 book ai didi

javascript - 如何在嵌套数组中实现乘法字段更新

转载 作者:行者123 更新时间:2023-11-30 20:15:56 26 4
gpt4 key购买 nike

我在 MongoDB 中有嵌套数组,并尝试更新第三级数组中的 multiply 值:

const query = {'_id': id};
const inc = {'$inc':
{'threads.$[thread].shifts.$[shift].services.$[service].balance': balance}
};
const arrayFilters = {
arrayFilters: [
{'thread.number': threadNumber},
{'shift.number':shiftNumber},
{'service.name': service},
]
};
await Vds.findOneAndUpdate(query, inc, arrayFilters);

在这种情况下,我只有一个字段更新 - 只有 balance,但我需要更新两个字段,balancelastUpdate(这是时间平衡更新)。

然后我被迫编写第二个查询:

const query = {'_id': id};
const inc = {'$inc':
{'threads.$[thread].shifts.$[shift].services.$[service].lastUpdate': new Date()}
};
const arrayFilters = {
arrayFilters: [
{'thread.number': threadNumber},
{'shift.number':shiftNumber},
{'service.name': service},
]
};
await Vds.findOneAndUpdate(query, inc, arrayFilters);

如何合并这些查询?像这样的东西:

const inc = {'$inc': [
{'threads.$[thread].shifts.$[shift].services.$[service].balance': balance},
{'threads.$[thread].shifts.$[shift].services.$[service].lastUpdate': new Date()},
]};

我在 the documentation 中看到过但没有找到类似的例子。

UPDATE_1

架构:

new Schema({

... some data
,
threads: [
{
number: {
type: Number,
required: true
},

shifts: [
{

... some data

number: {
type: Number,
required: true
},
services: [
{
name: {
type: String,
required: true
},
lastUpdate: {
type: Date,
default: null
},
balance: {
type: Number,
default: 0
}

... some data

}
]
}
]

}
]
});

UPDATE_2

示例对象:

{
"isCrash": false,
"_id": "5b79857b12f80c07168c88e5",
"user": "5b44df4b46c20409cf67dac5",
"regDate": "2018-08-19T14:58:03.030Z",
"threads": [
{
"shifts": [
{
"services": [
{
"lastUpdate": null,
"isCrash": true,
"balance": 27,
"_id": "5b79857b12f80c07168c8919",
},
{
"lastUpdate": null,
"isCrash": true,
"balance": 0,
"_id": "5b79857b12f80c07168c8918",
},
{
"lastUpdate": null,
"isCrash": true,
"balance": 0,
"_id": "5b79857b12f80c07168c8917",
}
],
"_id": "5b79857b12f80c07168c8916",
"number": 1
},
{
"services": [
{
"lastUpdate": null,
"isCrash": true,
"balance": 0,
"_id": "5b79857b12f80c07168c8915",
},
{
"lastUpdate": null,
"isCrash": true,
"balance": 0,
"_id": "5b79857b12f80c07168c8914",
"name": "VIP-IP"
},
{
"lastUpdate": null,
"isCrash": true,
"balance": 0,
"_id": "5b79857b12f80c07168c8913",
"name": "IP-WEB"
}
],
"_id": "5b79857b12f80c07168c8912",
"number": 2
}
],
"_id": "5b79857b12f80c07168c890d",
"number": 1
}
],
"endDate": "2019-09-18T17:22:57.373Z",
"note": "Test notes",
"__v": 0
}

最佳答案

这两个字段都应该在 $inc 运算符中

const query = { '_id': id }
const update = { '$inc': {
'threads.$[thread].shifts.$[shift].services.$[service].balance': balance,
'threads.$[thread].shifts.$[shift].services.$[service].lastUpdate': new Date()
}}
const arrayFilters = {
arrayFilters: [
{ 'thread.number': threadNumber },
{ 'shift.number' :shiftNumber },
{ 'service.name': service },
]
}
await Vds.findOneAndUpdate(query, update, arrayFilters)

或者如果你想$setlastUpdate字段

const query = { '_id': id }
const update = {
'$inc': { 'threads.$[thread].shifts.$[shift].services.$[service].balance': balance },
'$set': { 'threads.$[thread].shifts.$[shift].services.$[service].lastUpdate': new Date() }
}
const arrayFilters = {
arrayFilters: [
{ 'thread.number': threadNumber },
{ 'shift.number' :shiftNumber },
{ 'service.name': service },
]
}
await Vds.findOneAndUpdate(query, update, arrayFilters)

关于javascript - 如何在嵌套数组中实现乘法字段更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51919152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com