gpt4 book ai didi

MongoDB中数组内的正则表达式

转载 作者:IT老高 更新时间:2023-10-28 13:27:41 27 4
gpt4 key购买 nike

我想用正则表达式在 mongodb 的数组中进行查询,集合有这样的文档:

{
"_id" : ObjectId("53340d07d6429d27e1284c77"),
"company" : "New Company",
"worktypes" : [
{
"name" : "Pompas",
"works" : [
{
"name" : "name 2",
"code" : "A00011",
"price" : "22,22"
},
{
"name" : "name 3",
"code" : "A00011",
"price" : "22,22"
},
{
"name" : "name 4",
"code" : "A00011",
"price" : "22,22"
},
{
"code" : "asdasd",
"name" : "asdads",
"price" : "22"
},
{
"code" : "yy",
"name" : "yy",
"price" : "11"
}
]
},
{
"name" : "name 4",
"works" : [
{
"code" : "A112",
"name" : "Nombre",
"price" : "11,2"
}
]
},
{
"name" : "ee",
works":[

{
"code" : "aa",
"name" : "aa",
"price" : "11"
},
{
"code" : "A00112",
"name" : "Nombre",
"price" : "12,22"
}
]
}
]

}

然后我需要按公司名称查找文档,并且其中的任何工作都与代码或名称工作中的正则表达式匹配。我有这个:

var companyquery = { "company": "New Company"};
var regQuery = new RegExp('^A0011.*$', 'i');

db.categories.find({$and: [companyquery,
{$or: [
{"worktypes.works.$.name": regQuery},
{"worktypes.works.$.code": regQuery}
]}]})

但不返回任何结果..我认为错误是尝试使用 de dot 和 $..有什么想法吗?

编辑:

有了这个:

db.categories.find({$and: [{"company":"New Company"},
{$or: [
{"worktypes.works.name": {"$regex": "^A00011$|^a00011$"}},
{"worktypes.works.code": {"$regex": "^A00011$|^a00011$"}}
]}]})

这是结果:

{
"_id" : ObjectId("53340d07d6429d27e1284c77"),
"company" : "New Company",
"worktypes" : [
{
"name" : "Pompas",
"works" : [
{
"name" : "name 2",
"code" : "A00011",
"price" : "22,22"
},
{
"code" : "aa",
"name" : "aa",
"price" : "11"
},
{
"code" : "A00112",
"name" : "Nombre",
"price" : "12,22"
},
{
"code" : "asdasd",
"name" : "asdads",
"price" : "22"
},
{
"code" : "yy",
"name" : "yy",
"price" : "11"
}
]
},
{
"name" : "name 4",
"works" : [
{
"code" : "A112",
"name" : "Nombre",
"price" : "11,2"
}
]
},
{
"name" : "Bombillos"
},
{
"name" : "Pompas"
},
{
"name" : "Bombillos 2"
},
{
"name" : "Other type"
},
{
"name" : "Other new type"
}
]
}

正则表达式不显示结果 ok..

最佳答案

您正在使用 JavaScript 原生 RegExp 对象作为正则表达式,但是为了让 mongo 处理正则表达式,它需要作为查询文档的一部分发送,这不是一回事.

此外,正则表达式将与您想要的值不匹配。它实际上可能是精确匹配的 ^A0111$ ,但是您的不区分大小写的匹配会导致问题导致对可能的索引进行更大的扫描。所以有更好的写法。另请参阅文档链接以了解不区分大小写匹配的问题。

使用 $regex运算符:

db.categories.find({
"$and": [
{"company":"New Company"},
{ "$or": [
{ "worktypes.works.name": { "$regex": "^A00011$|^a00011$" }},
{ "worktypes.works.code": { "$regex": "^A00011$|^a00011$" }}
]}
]
})

此外,位置 $ 占位符对查询无效,它们仅用于投影或更新或查询找到的 first 匹配元素。 p>

但您的实际问题似乎是您试图仅获取与您的条件“匹配”的数组元素。您不能使用 .find() 执行此操作,为此您需要使用 .aggregate()而是:

db.categories.aggregate([

// Always makes sense to match the actual documents
{ "$match": {
"$and": [
{"company":"New Company"},
{ "$or": [
{ "worktypes.works.name": { "$regex": "^A00011$|^a00011$" }},
{ "worktypes.works.code": { "$regex": "^A00011$|^a00011$" }}
]}
]
}},

// Unwind the worktypes array
{ "$unwind": "$worktypes" },

// Unwind the works array
{ "$unwind": "$worktypes.works" },

// Then use match to filter only the matching entries
{ "$match": {
"$or": [
{ "worktypes.works.name": { "$regex": "^A00011$|^a00011$" } },
{ "worktypes.works.code": { "$regex": "^A00011$|^a00011$" } }
]
}},

/* Stop */
// If you "really" need the arrays back then include all the following
// Otherwise the steps up to here actually got you your results

// First put the "works" array back together
{ "$group": {
"_id": {
"_id": "$_id",
"company": "$company",
"workname": "$worktypes.name"
},
"works": { "$push": "$worktypes.works" }
}},

// Then put the "worktypes" array back
{ "$group": {
"_id": "$_id._id",
"company": { "$first": "$_id.company" },
"worktypes": {
"$push": {
"name": "$_id.workname",
"works": "$works"
}
}
}}
])

那又怎样.aggregate()对所有这些阶段所做的是将数组元素分解为正常的文档形式,以便可以使用 $match 运算符对其进行过滤。这样,只返回“匹配”的元素。

“查找”正确的做法是匹配符合条件的“文档”。由于文档包含匹配的元素,因此它们被返回。这两个原则是非常不同的东西。

当您的意思是“过滤”时,请使用聚合。

关于MongoDB中数组内的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22909081/

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