Javascript 如何在嵌套数组中实现多字段更新
我在MongoDB中有嵌套数组,并试图更新第三级数组中的乘法值:Javascript 如何在嵌套数组中实现多字段更新,javascript,mongodb,mongoose,Javascript,Mongodb,Mongoose,我在MongoDB中有嵌套数组,并试图更新第三级数组中的乘法值: const query = {'_id': id}; const inc = {'$inc': {'threads.$[thread].shifts.$[shift].services.$[service].balance': balance} }; const arrayFilters = { arrayFilters: [ {'thread.number': threadNumber},
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);
在本例中,我只更新了一个字段-仅余额
,但我需要更新两个字段,余额
和上次更新
(这是时间余额更新)
我不得不写第二个查询:
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()},
]};
我在中见过,但没有找到类似的例子
更新_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
}
]
}
]
}
]
});
更新_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)
或者,如果要$set
,请单击lastUpdate
字段
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)
在第二种情况下,使用了
$set
我得到了更新路径'threads.$[thread].shift.$[shift].services.$[service].balance'会在'threads.$[thread].shift.$[shift].services.$[service].balance'
处产生冲突。在第一个使用$inc
的情况下,我得到的无法使用非数字参数递增:{threads.$[thread].shift.$[shift].services.$[service].lastUpdate:new Date(1534699306238)}
可能是我的lastUpdate的默认值null
?lastUpdate的数据类型是什么?类型Date
我在问题中添加了模式。可能Date
类,对于MongoDB中的日期容器不是一个好的选择?我刚刚检查了它是否适合我。。。你能把你收集的样品寄出去吗