【发布时间】:2015-03-12 01:27:33
【问题描述】:
我们正在查询返回的结果应该是建议的搜索词列表。
我们目前有一个查询可以检查多个字段的正则表达式匹配:
$or:[
{'description.position':/s/i},
{'employer.name':/s/i},
{'hiringManager.profile.name':/s/i}
]
我们希望返回的结果是唯一的匹配数组(不重复)。
返回的结果类似于:
I20150311-18:17:14.151(-7)? "fields": {
I20150311-18:17:14.154(-7)? "hiringManager": {
I20150311-18:17:14.157(-7)? "profile": {
I20150311-18:17:14.160(-7)? "name": "Seth Sandler"
I20150311-18:17:14.163(-7)? }
I20150311-18:17:14.167(-7)? },
I20150311-18:17:14.173(-7)? "description": {
I20150311-18:17:14.177(-7)? "position": "Cook"
I20150311-18:17:14.181(-7)? },
I20150311-18:17:14.187(-7)? "employer": {
I20150311-18:17:14.191(-7)? "name": "Employer"
I20150311-18:17:14.195(-7)? },
I20150311-18:17:14.206(-7)? }
I20150311-18:17:14.209(-7)? }
I20150311-18:17:14.212(-7)? {
I20150311-18:17:14.223(-7)? "fields": {
I20150311-18:17:14.226(-7)? "hiringManager": {
I20150311-18:17:14.229(-7)? "profile": {
I20150311-18:17:14.232(-7)? "name": "Seth Sandler"
I20150311-18:17:14.234(-7)? }
I20150311-18:17:14.237(-7)? },
I20150311-18:17:14.240(-7)? "description": {
I20150311-18:17:14.243(-7)? "position": "Cook"
I20150311-18:17:14.246(-7)? },
I20150311-18:17:14.249(-7)? "employer": {
I20150311-18:17:14.252(-7)? "name": "Employer 4"
I20150311-18:17:14.254(-7)? },
I20150311-18:17:14.264(-7)? }
I20150311-18:17:14.267(-7)? }
I20150311-18:17:14.269(-7)? {
I20150311-18:17:14.281(-7)? "fields": {
I20150311-18:17:14.284(-7)? "hiringManager": {
I20150311-18:17:14.287(-7)? "profile": {
I20150311-18:17:14.290(-7)? "name": "Seth Sandler"
I20150311-18:17:14.293(-7)? }
I20150311-18:17:14.295(-7)? },
I20150311-18:17:14.298(-7)? "description": {
I20150311-18:17:14.301(-7)? "position": "Chef"
I20150311-18:17:14.304(-7)? },
I20150311-18:17:14.307(-7)? "employer": {
I20150311-18:17:14.310(-7)? "name": "Emplopyer 3"
I20150311-18:17:14.313(-7)? },
I20150311-18:17:14.321(-7)? }
I20150311-18:17:14.323(-7)? }
I20150311-18:17:14.325(-7)? {
I20150311-18:17:14.334(-7)? "fields": {
I20150311-18:17:14.336(-7)? "hiringManager": {
I20150311-18:17:14.338(-7)? "profile": {
I20150311-18:17:14.340(-7)? "name": "Seth Sandler"
I20150311-18:17:14.342(-7)? }
I20150311-18:17:14.344(-7)? },
I20150311-18:17:14.346(-7)? "description": {
I20150311-18:17:14.348(-7)? "position": "Chef"
I20150311-18:17:14.350(-7)? },
I20150311-18:17:14.353(-7)? "employer": {
I20150311-18:17:14.356(-7)? "name": "Employer"
I20150311-18:17:14.359(-7)? },
I20150311-18:17:14.366(-7)? }
I20150311-18:17:14.369(-7)? }
我们希望结果是一个唯一的数组,包含hiringManager.profile.name、employer.name 和description.position 的值。
我们当前的解决方案似乎并不理想(可能性能不佳),并且想知道是否可以使用 mongogodb 聚合函数将字段值放入数组中。
当前解决方案(不理想):
aggregate([
{$match: {$or:[ {'description.position':/s/i}, {'employer.name':/s/i}, {'hiringManager.profile.name':/s/i} ]}},
{$group: {_id: 1, positions: {$push: '$description.position'}, employerNames: {$push: '$employer.name'}, hiringManagerNames: {$push:'$hiringManager.profile.name'}}},
{$project: {_id:1, texts: {$setUnion: ['$positions', {$setUnion: ['$employerNames', '$hiringManagerNames']}]}}}
])
})
这个输出是正确的,但是我们想要一个更好的聚合函数来限制结果。
I20150311-18:25:26.461(-7)? "result": [
I20150311-18:25:26.465(-7)? {
I20150311-18:25:26.468(-7)? "_id": 1,
I20150311-18:25:26.472(-7)? "texts": [
I20150311-18:25:26.478(-7)? "Employer 5",
I20150311-18:25:26.481(-7)? "Employer 4",
I20150311-18:25:26.485(-7)? "Employer 1",
I20150311-18:25:26.488(-7)? "Manager",
I20150311-18:25:26.504(-7)? "Cook",
I20150311-18:25:26.507(-7)? "Chef",
I20150311-18:25:26.530(-7)? ]
I20150311-18:25:26.534(-7)? }
I20150311-18:25:26.538(-7)? ]
【问题讨论】:
-
所以你的问题是结果只是一个大文档,你只需要响应中的“不同”“文本”值。对吗?
-
没错。问题是不同的值来自 3 个不同的字段(因为我们正在查询 3 个字段以进行正则表达式匹配)。
标签: regex mongodb mongodb-query aggregation-framework