如何查找与查询中数组条目完全相同的文档 [英] How to find documents with exactly the same array entries as in a query

查看:79
本文介绍了如何查找与查询中数组条目完全相同的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个集合中有文档,看起来像这样:

I have documents in a collection, looking like this:

[
  {
    userId: 1,
    itemsIds: [399957190, 366369952],
    hash: '85e765840b1cd3c413404cdf6b8fb2a4'
  },
  {
    userId: 2,
    itemsIds: [349551151, 366369952],
    hash: 'a28fa334515749b1b13fcd2183edb8de'
  },
  {
    userId: 3,
    itemsIds: [399957190, 366369952],
    hash: '85e765840b1cd3c413404cdf6b8fb2a4'
  }

]

这些是用户,他们的列表中有喜欢的项目.我想将一个用户的列表发送给其他用户,然后查找它们是否相等.如果是这样,我想在我的代码中将它们标记为一对,并执行一些操作.

These are users, which have favorite items in their lists. I want one user's list to others and find if they are equal. If they are, I want to mark them as a kind of a pair in my code and perform some actions.

在上面的示例中,用户1和3具有相同的收藏夹列表. 如何找到带有数组的用户,该数组恰好包含我列出的值?

In the example above users 1 and 3 have the same favorites lists. How do I find users with an array which contains exactly the values I list?

推荐答案

这里有几种非常有用的情况",实际上试图在数组内容上创建唯一哈希"实际上是无处不在"容易解决的众多问题.

There are several "very useful cases" here where in fact trying to create a "unique hash" over the array content is actually "getting in the way" of the myriad of problems that can be easily addressed.

例如,如果您从提供的示例中获取用户1",并认为您已经加载了该数据,并希望通过匹配的"itemsIds"从当前用户对象的内容中找到与我共同的人" ,那么有两种简单的查询方法:

If you for example take "user 1" from the sample provided, and consider that you have that data loaded already and want to find "those in common with me" by the matched "itemsIds" from what the current user object has, then there are two simple query approaches:

  1. 完全相同"地找到:在此处您要检查其他用户数据以查看那些具有完全相同"兴趣的用户.这是 $all 的简单且无序"的用法>查询运算符:

  1. Find "exactly" the same: Is where you want to inspect other user data to see those users that have the same "exact" interests. This is simple and "unordered" usage of the $all query operator:

db.collection.find({ 
    "itemsIds": { "$all": [399957190, 366369952] },
    "userId": { "$ne": 1 }
})

这将返回用户3",因为它们是具有两者"共同的"itemsIds"条目的用户.顺序在这里并不重要,因为只要两者都存在,它就始终是任何顺序中的匹配项.这是$and作为查询参数的另一种形式.

Which is going to return "user 3" since they are the one with "both" common "itemsIds" entries. Order is not important here as it is always a match in any order, as long as they are both there. This is another form of $and as query arguments.

找到与我有共同点的相似之处":基本上是在问您有相同的东西吗?" .为此,您可以使用 $in 查询运算符.如果满足指定条件的任意":

Find "similar" in common to me": Which is basically asking "do you have something that is the same?". For that you can use the $in query operator. It will match if "either" of the specified conditions is met:

db.collection.find({ 
    "itemsIds": { "$in": [399957190, 366369952] },
    "userId": { "$ne": 1 }
})

在这种情况下,用户2"和用户3"都将匹配,因为它们至少"共享指定条件中的一个",这意味着与用户2"和用户3"具有共同点"查询的源数据.

In this case "both" the "user 2" and "user 3" are going to match, as they "at least" share "one" of the conditions specified and that means that have "something in common" with the source data of the query.

这实际上是$or查询运算符的另一种形式,就像以前,在要应用条件的情况下,编写这种方式要简单得多且简洁.

This is in fact another form of the $or query operator, and just like before it is a lot simplier and concise to write this way given the conditions to be applied.

发现常见的事物"

在某些情况下,您可能想找到共同点"而又没有基础的用户"作为起点.那么,如何分辨用户1"和用户2"共享相同的"itemIds",或者实际上,各个用户可能分别共享相同的"itemIds"值,但它们又是谁呢?

Finding Common "Things"

There are also cases where you might want to find things "in common" without having a base "user" to start from. So how do you tell that "user 1" and "user 2" share the same "itemIds", or in fact that various of the users might share the same "itemIds" value individually, but who are they?

  1. 获取精确匹配项:当然,您可以在其中查看"itemsIds"值和

  1. Get the exact matches: Is of course where you look at the "itemsIds" values and $group them together. Generally the "order is important" here, so optimally you have them "pre-ordered" and consistently always to make this as simple as:

db.collection.aggregate([
    { "$group": {
        "_id": "$itemsIds",
        "common": { "$push": "$userId" }
    }}
])

只要订单已经存在,这就是它的全部内容.如果没有,那么您可以做一个稍长的缠绕形式来进行排序",但是对于生成哈希"也可以这样说:

And that is all there really is to it, as long as the order is already there. If not, then you can do a slightly longer winded form to do the "ordering", but the same could be said of generating a "hash":

db.collection.aggregate([
    { "$unwind": "$itemsIds" },
    { "$sort": { "_id": 1, "itemsIds": 1 } },
    { "$group": {
        "_id": "$_id",
        "userId": { "$first": "$userId" },
        "itemsIds": { "$push": "$itemsIds" }
    }},
    { "$group": {
        "_id": "$itemsIds",
        "common": { "$push": "$userId" }
    }}
])

不是超级"性能,但它说明了为什么总是在添加数组条目时保持有序.这是一个非常简单的过程.

Not "super" performant, but it makes the point of why you always keep ordered on addition of array entries. Which is a very simple process.

将用户"与项目"共用:这是上面另一个抽象的简单过程,其中分解"了$unwind下的阵列,然后基本上归为一类:

Common "user" to "items": Which is another simple process abstracting on above with "breaking down" the array under $unwind, and then basically grouping back:

db.collection.aggregate([
    { "$unwind": "$itemsIds" },
    { "$group": {
        "_id": "$itemsIds",
        "users": { "$addToSet": "$userId" }
    }}
])

再次,只是 $addToSet 完成工作,并为每个"itemsIds"值收集唯一的userId"值.

And again, just a simple grouping aggregator of $addToSet does the job and collects the "distinct userId" values for each "itemsIds" value.

这些都是基本的解决方案,我可以继续使用设置交集",但不可以,但这就是入门".

These are all basic solutions, and I could go on with "set intersections" and what not, but this is the "primer".

不要尝试计算哈希",无论如何,MongoDB都有一个很好的武器库"来匹配条目.使用它并滥用"它,直到它破裂.然后再努力.

Don't try to compute a "hash", MongoDB has a good "arsenal" for matching the entries anyway. Use it and "abuse it" as well, until it breaks. Then try harder.

这篇关于如何查找与查询中数组条目完全相同的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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