我正在构建一个可以比作约会应用程序的应用程序。
我有一些结构如下的文件:
$ db.profiles.find().pretty()
[
{
"_id": 1,
"firstName": "John",
"lastName": "Smith",
"fieldValues": [
"favouriteColour|red",
"food|pizza",
"food|chinese"
]
},
{
"_id": 2,
"firstName": "Sarah",
"lastName": "Jane",
"fieldValues": [
"favouriteColour|blue",
"food|pizza",
"food|mexican",
"pets|yes"
]
},
{
"_id": 3,
"firstName": "Rachel",
"lastName": "Jones",
"fieldValues": [
"food|pizza"
]
}
]
我正在尝试的是识别在一个或多个上相互匹配的配置文件fieldValues
。
因此,在上面的示例中,我的理想结果如下所示:
<some query>
result:
[
{
"_id": "507f1f77bcf86cd799439011",
"dateCreated": "2013-12-01",
"profiles": [
{
"_id": 1,
"firstName": "John",
"lastName": "Smith",
"fieldValues": [
"favouriteColour|red",
"food|pizza",
"food|chinese"
]
},
{
"_id": 2,
"firstName": "Sarah",
"lastName": "Jane",
"fieldValues": [
"favouriteColour|blue",
"food|pizza",
"food|mexican",
"pets|yes"
]
},
]
},
{
"_id": "356g1dgk5cf86cd737858595",
"dateCreated": "2013-12-02",
"profiles": [
{
"_id": 1,
"firstName": "John",
"lastName": "Smith",
"fieldValues": [
"favouriteColour|red",
"food|pizza",
"food|chinese"
]
},
{
"_id": 3,
"firstName": "Rachel",
"lastName": "Jones",
"fieldValues": [
"food|pizza"
]
}
]
}
]
我考虑过将其作为 map reduce 或使用聚合框架来实现。
无论哪种方式,“结果”都会被保存到一个集合中(根据上面的“结果”)
我的问题是两者中哪一个更适合?我将从哪里开始实施呢?
编辑
简而言之,模型不能轻易更改。
这不像传统意义上的“个人资料”。
我基本上想要做的(在伪代码中)是这样的:
foreach profile in db.profiles.find()
foreach otherProfile in db.profiles.find("_id": {$ne: profile._id})
if profile.fieldValues matches any otherProfie.fieldValues
//it's a match!
显然那种操作非常非常慢!
值得一提的是,这些数据从未显示,它实际上只是一个用于“匹配”的字符串值