gpt4 book ai didi

MongoDB - 如果不满足 arrayFilters,则对数组执行 upsert

转载 作者:行者123 更新时间:2023-12-04 11:15:23 25 4
gpt4 key购买 nike

我在 mongo 中存储了以下文件:

{
"_id" : ObjectId("5d1a08d2329a3c1374f176df"),
"associateID" : "1234567",
"associatePreferences" : [
{
"type" : "NOTIFICATION",
"serviceCode" : "service-code",
"eventCode" : "test-template",
"preferences" : [
"TEXT",
"EMAIL"
]
},
{
"type" : "URGENT_NOTIFICATION",
"serviceCode" : "service-code",
"eventCode" : "test-template",
"preferences" : [
"TEXT"
]
}
]
}
我基本上试图根据它的类型、serviceCode 和 eventCode 查询 associatePreferences 数组的元素之一,并向首选项数组添加一个新值。但是,如果类型、serviceCode 和 eventCode 的组合不存在,我想向带有这些值的 associatePreferences 数组添加一个新元素。这是我当前的查询:
db.user_communication_preferences.update(
{'associateID':'testassociate'},
{$addToSet:{'associatePreferences.$[element].preferences':"NEW_VALUE"}},
{arrayFilters:[{'element.serviceCode':'service-code-not-present', 'element.eventCode':'event-code-not-present','element.type':'URGENT_NOTIFICATION'}]}
)
如果所有 arrayFilters 都存在于 associatePreferences 的元素中,则此查询有效,但如果不存在,则不会添加新元素。我错过了什么?

最佳答案

您可以使用聚合管道来检查元素是否存在,然后将该元素附加到 associatePreferences有条件地排列。最后,使用聚合结果更新您的文档。

db.user_communication_preferences.aggregate([
{
"$match": {
"associateID": "testassociate"
}
},
{
"$addFields": {
"filteredArray": {
"$filter": {
"input": "$associatePreferences",
"as": "pref",
"cond": {
$and: [
{
$eq: [
"$$pref.type",
"URGENT_NOTIFICATION"
]
},
{
$eq: [
"$$pref.eventCode",
"event-code-not-exists"
]
},
{
$eq: [
"$$pref.serviceCode",
"service-code-not-exists"
]
}
]
}
}
}
}
},
{
$addFields: {
"needAddElement": {
$eq: [
{
"$size": "$filteredArray"
},
0
]
}
}
},
{
"$addFields": {
"associatePreferences": {
"$concatArrays": [
"$associatePreferences",
{
"$cond": {
"if": {
$eq: [
"$needAddElement",
true
]
},
"then": [
{
"type": "URGENT_NOTIFICATION",
"serviceCode": "service-code-not-exists",
"eventCode": "event-code-not-exists",
"preferences": [
"TEXT"
]
}
],
"else": []
}
}
]
}
}
}
]).forEach(result){
db.user_communication_preferences.update({
_id : result._id
}, {
$set: {
"associatePreferences" : result.associatePreferences
}
})
}

关于MongoDB - 如果不满足 arrayFilters,则对数组执行 upsert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56855306/

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