mongodb检查从一个集合到另一个集合中所有字段的正则表达式 [英] mongodb check regex on fields from one collection to all fields in other collection
问题描述
在研究google和SO一周后,我最终在这里提出了问题.假设有两个集合,
After digging google and SO for a week I've ended up asking the question here. Suppose there are two collections,
UsersCollection:
UsersCollection:
[
{...
name:"James"
userregex: "a|regex|str|here"
},
{...
name:"James"
userregex: "another|regex|string|there"
},
...
]
PostCollection:
PostCollection:
[
{...
title:"a string here ..."
},
{...
title: "another string here ..."
},
...
]
我需要获取所有userregex
将与任何post.title
匹配的用户(需要user_id,post_id组或类似名称).
I need to get all users whose userregex
will match any post.title
(Need user_id, post_id groups or something similar).
到目前为止,我已经尝试过:
1.收集所有用户,对所有产品运行正则表达式,可以运行,但是太脏了!它必须为每个用户执行查询
2.与上面相同,但在Mongo查询中使用foreach,与上面相同,但仅数据库层而不是应用程序层
What I've tried so far:
1. Get all users in collection, run regex on all products, works but too dirty! it'll have to execute a query for each user
2. Same as above, but using a foreach in Mongo query, it's the same as above but only Database layer instead of application layer
我在没有运气的情况下大量搜索了可用的方法,例如聚合,逆风等.
那么在Mongo可以做到这一点吗?我应该更改数据库类型吗?如果是,哪种类型会比较好?性能是我的首要任务.谢谢
I searched alot for available methods such as aggregations, upwind etc with no luck.
So is it possible to do this in Mongo? Should i change my database type? if yes what type would be good? performance is my first priority. Thanks
推荐答案
无法在匹配表达式内的regex运算符中引用存储在文档中的regex字段.
It is not possible to reference the regex field stored in the document in the regex operator inside match expression.
因此,使用当前结构无法在mongo端完成此操作.
So it can't be done in mongo side with current structure.
$lookup
在相等条件下工作良好.因此,一种替代方法(类似于Nic的建议)是更新您的帖子集,为每个标题添加一个名为keywords
(可以在其上搜索的关键字值的数组)的额外字段.
$lookup
works well with equality condition. So one alternative ( similar to what Nic suggested ) would be update your post collection to include an extra field called keywords
( array of keyword values it can be searched on ) for each title.
db.users.aggregate([
{$lookup: {
from: "posts",
localField: "userregex",
foreignField: "keywords",
as: "posts"
}
}
])
上面的查询将执行类似的操作(从3.4开始).
The above query will do something like this (works from 3.4).
keywords: { $in: [ userregex.elem1, userregex.elem2, ... ] }.
来自文档
如果该字段包含一个数组,则$ in运算符选择 字段包含至少包含一个的数组的文档 与指定数组中的值匹配的元素(例如, 等)
If the field holds an array, then the $in operator selects the documents whose field holds an array that contains at least one element that matches a value in the specified array (e.g. , , etc.)
只有在数组具有相同的顺序,数组的值和长度相同的情况下,早期版本(在3.2上进行测试)才看起来匹配.
It looks like earlier versions ( tested on 3.2 ) will only match if array have same order, values and length of arrays is same.
样本输入:
用户
db.users.insertMany([
{
"name": "James",
"userregex": [
"another",
"here"
]
},
{
"name": "John",
"userregex": [
"another",
"string"
]
}
])
帖子
db.posts.insertMany([
{
"title": "a string here",
"keyword": [
"here"
]
},
{
"title": "another string here",
"keywords": [
"another",
"here"
]
},
{
"title": "one string here",
"keywords": [
"string"
]
}
])
样本输出:
[
{
"name": "James",
"userregex": [
"another",
"here"
],
"posts": [
{
"title": "another string here",
"keywords": [
"another",
"here"
]
},
{
"title": "a string here",
"keywords": [
"here"
]
}
]
},
{
"name": "John",
"userregex": [
"another",
"string"
],
"posts": [
{
"title": "another string here",
"keywords": [
"another",
"here"
]
},
{
"title": "one string here",
"keywords": [
"string"
]
}
]
}
]
这篇关于mongodb检查从一个集合到另一个集合中所有字段的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!