MongoDB 查询以在第三级对象数组中查找文本
MongoDB query to find text in third level array of objects
我有一个 Mongo 集合,其中包含 Vue/Laravel 应用程序中已保存搜索的数据,它包含如下记录:
{
"_id" : ObjectId("6202f3357a02e8740039f343"),
"q" : null,
"name" : "FCA last 3 years",
"frequency" : "Daily",
"scope" : "FederalContractAwardModel",
"filters" : {
"condition" : "AND",
"rules" : [
{
"id" : "awardDate",
"operator" : "between_relative_backward",
"value" : [
"now-3.5y/d",
"now/d"
]
},
{
"id" : "subtypes.extentCompeted",
"operator" : "in",
"value" : [
"Full and Open Competition"
]
}
]
},
问题是 rules
数组中项目的 value
有小数。
"value" : [
"now-3.5y/d",
"now/d"
]
特别是小数点。由于 UI 错误,允许用户输入十进制值,因此需要修复此问题以像这样删除小数。
"value" : [
"now-3y/d",
"now/d"
]
我的问题是编写一个 Mongo 查询来识别这些记录(我是一个 Mongo 菜鸟)。我需要的是识别此集合中的记录,这些记录在 filters.rules
数组中有一个项目,在“value”数组中有一个项目包含小数。
小菜一碟,对吧?
这是我所知道的。
myCollection.find({"filters.rules": })
但我不确定从这里到哪里去。
更新:在@R2D2 提供的 运行 正则表达式之后,我发现它还会显示带有有效日期字符串的记录,例如
"rules" : [
{
"id" : "dueDate",
"operator" : "between",
"value" : [
"2018-09-10T19:04:00.000Z",
null
]
},
所以我需要做的是过滤掉句点两边都有双 0 的情况(即 00.00
)。如果我正确阅读正则表达式,这部分
[^\.]
不包括字符,所以我想要像
这样的东西
[^00\.00]
但是运行这个查询
db.collection.find( {
"filters.rules.value": { $regex: /\.[^00\.00]*/ }
} )
仍然 returns 相同的记录,即使它在正则表达式测试器中按预期工作。我错过了什么?
要查找包含至少一个带有 (.) 的值字符串的所有文档,请尝试:
db.collection.find( {
"filters.rules.value": { $regex: /\.[^\.]*/ }
} )
或者您可以仅过滤需要通过聚合修复的字段,如下所示:
[direct: mongos]> db.tes.aggregate([ {$unwind:"$filters.rules"}, {$unwind:"$filters.rules.value"}, {$match:{ "filters.rules.value": {$regex: /\.[^\.]*/ } }} ,{$project:{_id:1,oldValue:"$filters.rules.value"}} ])
[
{ _id: ObjectId("6202f3357a02e8740039f343"), oldValue: 'now-3.5y/d' }
]
[direct: mongos]>
稍后更新这些值:
db.collection.update({
"filters.rules.value": "now-3.5y/d"
},
{
$set: {
"filters.rules.$[x].value.$": "now-3,5y/d-CORRECTED"
}
},
{
arrayFilters: [
{
"x.value": "now-3.5y/d"
}
]
})
我有一个 Mongo 集合,其中包含 Vue/Laravel 应用程序中已保存搜索的数据,它包含如下记录:
{
"_id" : ObjectId("6202f3357a02e8740039f343"),
"q" : null,
"name" : "FCA last 3 years",
"frequency" : "Daily",
"scope" : "FederalContractAwardModel",
"filters" : {
"condition" : "AND",
"rules" : [
{
"id" : "awardDate",
"operator" : "between_relative_backward",
"value" : [
"now-3.5y/d",
"now/d"
]
},
{
"id" : "subtypes.extentCompeted",
"operator" : "in",
"value" : [
"Full and Open Competition"
]
}
]
},
问题是 rules
数组中项目的 value
有小数。
"value" : [
"now-3.5y/d",
"now/d"
]
特别是小数点。由于 UI 错误,允许用户输入十进制值,因此需要修复此问题以像这样删除小数。
"value" : [
"now-3y/d",
"now/d"
]
我的问题是编写一个 Mongo 查询来识别这些记录(我是一个 Mongo 菜鸟)。我需要的是识别此集合中的记录,这些记录在 filters.rules
数组中有一个项目,在“value”数组中有一个项目包含小数。
小菜一碟,对吧?
这是我所知道的。
myCollection.find({"filters.rules": })
但我不确定从这里到哪里去。
更新:在@R2D2 提供的 运行 正则表达式之后,我发现它还会显示带有有效日期字符串的记录,例如
"rules" : [
{
"id" : "dueDate",
"operator" : "between",
"value" : [
"2018-09-10T19:04:00.000Z",
null
]
},
所以我需要做的是过滤掉句点两边都有双 0 的情况(即 00.00
)。如果我正确阅读正则表达式,这部分
[^\.]
不包括字符,所以我想要像
这样的东西[^00\.00]
但是运行这个查询
db.collection.find( {
"filters.rules.value": { $regex: /\.[^00\.00]*/ }
} )
仍然 returns 相同的记录,即使它在正则表达式测试器中按预期工作。我错过了什么?
要查找包含至少一个带有 (.) 的值字符串的所有文档,请尝试:
db.collection.find( {
"filters.rules.value": { $regex: /\.[^\.]*/ }
} )
或者您可以仅过滤需要通过聚合修复的字段,如下所示:
[direct: mongos]> db.tes.aggregate([ {$unwind:"$filters.rules"}, {$unwind:"$filters.rules.value"}, {$match:{ "filters.rules.value": {$regex: /\.[^\.]*/ } }} ,{$project:{_id:1,oldValue:"$filters.rules.value"}} ])
[
{ _id: ObjectId("6202f3357a02e8740039f343"), oldValue: 'now-3.5y/d' }
]
[direct: mongos]>
稍后更新这些值:
db.collection.update({
"filters.rules.value": "now-3.5y/d"
},
{
$set: {
"filters.rules.$[x].value.$": "now-3,5y/d-CORRECTED"
}
},
{
arrayFilters: [
{
"x.value": "now-3.5y/d"
}
]
})