1

I am tracking user IP's by adding them to an array on the user document like so:

{
  "_id": "LafnHzmQL6rBmXNxJ",
  "name": "someuser",
  "displayName": "SomeUser",
  "knownIPs": ["1.1.1.1", "2.2.2.2", "3.3.3.3"]
}

How can I find any documents where any 1 of the knownIPs match any 1 of the knownIPs in another (without specifying a particular value) regardless of what the actual IP is.

The goal is to identify people who are using multiple accounts so they can be flagged programmatically for further inspection. I have over 40,000 users, will this be too intensive of an aggregation?

2

2 Answers 2

2

Use the following aggregation pipeline:

db.collection.aggregate([
    { "$unwind": "$knownIPs" },
    {
        "$group": {
            // Group by the IP address
            "_id": "$knownIPs",

            // Count number of matching docs for the group
            "count": { "$sum":  1 },

            // Save the _id for matching docs
            "docs": { "$push": "$_id" }
        }
    },
    {
        "$match": {
            "count": { "$gt": 1 }
        }
    }
])
Sign up to request clarification or add additional context in comments.

Comments

1

I think you should use $unwind function on the array of knownIPs. so that it will give you three child object from parent object.

For example:

db.document_name.aggregate( [ { $unwind : "$knownIPs" } ] ) gives you

{
  "_id": "LafnHzmQL6rBmXNxJ",
  "name": "someuser",
  "displayName": "SomeUser",
  "knownIPs": "1.1.1.1"
}

{
  "_id": "LafnHzmQL6rBmXNxJ",
  "name": "someuser",
  "displayName": "SomeUser",
  "knownIPs": "2.2.2.2"
}

{
  "_id": "LafnHzmQL6rBmXNxJ",
  "name": "someuser",
  "displayName": "SomeUser",
  "knownIPs": "3.3.3.3"
}

After unwind operation, perform GroupBy operation on all the objects generated from unwind operation. So it will give the number of users who have same Ip.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.