MongoDB比较数组 [英] MongoDB compare arrays

查看:375
本文介绍了MongoDB比较数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有数千名用户的收藏集.每个用户文档都有一些属性,例如名称,年龄范围和收藏夹.

I have a collection with thousands of users. Each user document has a few properties like name, age range, and favourites.

我可以轻松地帮助用户找到相同年龄段的其他用户,但我也想以某种方式允许他们选择与其他用户共同拥有的收藏夹的匹配百分比或数量.

I can easily help users find other users that are the within the same age range but I also want to somehow allow them to choose a matching percentage OR number of the favourites they have in common with other users.

例如.

User 1
Name: x
Age Range: 19-25
Favourites: ["Red", "Green", "Blue"]

User 2
Name: y
Age Range: 19-25
Favourites: ["Orange", "Red", "Pink"]

User 3
Name: z
Age Range: 19-25
Favourites: ["Orange", "Red", "Blue"]

在这里,如果用户1搜索匹配度为33%或至少一个常见匹配项的用户,则他们只会获得用户2.如果搜索1%匹配度为66%或至少有两个常规匹配项的用户,那么他们将获得用户3.

Here, if user 1 searched for users with a 33% match OR at least one common match, they would get only user 2. If they searched for a 66% OR at least two common matches, they would get user 3.

我已经完成了简单的部分,并按年龄段进行了匹配,并尝试使用mongoDB $all$in,但这并不是我想要的.有人可以指出我正确的方向吗?

I've done the easy part and matched by age range and tried to work with mongoDB $all and $in but it's not exactly what I'm looking for. Can someone point me in the right direction?

推荐答案

好吧,如果您要查找具有某些公共属性的对象,我将采用另一种方法.我将在属性上创建全文本索引.在您的特定情况下为Favorites.

Well, if you want to find objects having some common attributes, I'll take a different approach. I'll create full text index on attribute. In your particular case it's Favorites.

搜索文本时,全文索引要快得多.它还为您提供了文本分数,其中显示了给定术语在集合中匹配的数量.

Full text index is much faster when search for text. It also gives you a text score showing how much a given term is matching in collection.

在您的特定情况下,我将进行text score评估,以查看其他文档是否符合我的标准.

In your particular case, I'll be measuring text score to see if other documents are falling with in my criteria.

您需要先创建full text索引.

db.collection.createIndex({"Favourites":"text"})

在创建全文索引之后,假设您尝试查找所有具有至少66%个匹配项的文档.这意味着如果我们有三个文本术语,则我们希望所有文档都匹配至少三个术语中的两个.

After creation of full text index, assuming you are trying to find all documents with at least 66% match. It means if we have three text terms, we want all documents matching at least two out of 3 terms.

var match = 2;
var terms = "Red Green Blue";

db.collection.aggregate([
 { $match: { $text: { $search: terms } } },
 { $project: {User:1, _id:0, Name:1, "Age Range":1, Favourites:1, score: {$meta: "textScore"}}},
 { $sort: { score: 1 }},
 { $match: { score: { $gte: match } } }
])

在上面的示例中,我们希望找到所有带有至少两个匹配术语的文档.上面的代码段将返回:

In example above, we want to find all documents with at least two matching terms. Code snippet above will return:

{ 
    "User" : 3.0, 
    "Name" : "z", 
    "Age Range" : "19-25", 
    "Favourites" : [
        "Orange", 
        "Red", 
        "Blue"
    ], 
    "score" : 2.2
}
{ 
    "User" : 1.0, 
    "Name" : "x", 
    "Age Range" : "19-25", 
    "Favourites" : [
        "Red", 
        "Green", 
        "Blue"
    ], 
    "score" : 3.3000000000000003
}

我们取回了至少两个词匹配的两个文档.

We got back two documents matching at least two terms.

更新:

OP提到术语可以包含多词短语. MongoDB全文允许搜索短语,并要求将短语用字符串引号引起来.

OP mentioned that terms can contain multi-word phrase. MongoDB full text allows to search for phrases and requires to wrap phrases around with string quotes.

例如var terms = "Red \"Light Blue\"";

使用上面的代码段,并提供包含"c7>"短语的收藏夹"短语的文档,将返回匹配的文档.

Using above code snippet and provided that document contains Light Blue phrase in Favourites, will return matching document.

但是有一个陷阱. MongoDB始终对带有其余术语(如果有)的短语执行logical and操作.在上面的示例中,代码将搜索包含短语Light Blue和术语Red

However there is a catch. MongoDB always perform logical and operation on phrases with rest of terms if any. In example above, code will search a document which contains phrase Light Blue and term Red

请参阅 https://docs.mongodb.com/manual /reference/operator/query/text/#phrases

这篇关于MongoDB比较数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆