我有quiz1,quiz2和quiz3,我想向用户推荐一个测验,根据quiz3中的标签进行quiz1或quiz2测试而不采用集合中的每个文档并在应用程序级别上处理它们。
Quiz_1 = {
_id:"...",
tags:["life-style","personality","sports","soccer"]
};
Quiz_2 = {
_id:"...",
tags:["IQ","inteligence","science","maths"]
};
Quiz_3 = {
_id:"...",
tags:["life-style","maths","inteligence","school"]
};
此处的最佳匹配是quiz_2,因为quiz_3将quiz_2与2个标记匹配,并且仅与1个标记匹配quiz_1。 (更多标签匹配等于更好的匹配)我如何使用mongoose?
说明:
我有一个名为" quizzes"我希望mongodb在我输入时返回第二个文件(标签:["生活方式","数学","智能","学校& #34;])
quizzes:
[
{
_id:...,
tags:["life-style","personality","sports","soccer"]]
},
{
_id:"...",
tags:["IQ","inteligence","science","maths"]
},
{
_id:"...",
tags:["life-style","maths","inteligence","school"]
}
]
答案 0 :(得分:1)
基本上,您希望获得比较数组$size
的$setIntersection
和文档中的"tags"
数组。然后,您$sort
将最大的“尺寸”放在最上面,最后$limit
得到一个结果:
var compare = ["life-style","maths","inteligence","school"];
Quizzes.aggregate([
{ "$match": { "tags": { "$in": compare } } },
{ "$project": {
"size": {
"$size": {
"$setIntersection": [ compare, "$tags" ]
}
}
}},
{ "$sort": { "size": -1 } },
{ "$limit": 1 }
])
通过使用$in
仅检查实际上至少有一个匹配项的文档来加快速度。