0

I have an intermediate experience of Mongodb, and Javascript is not a language I master a lot.
I used the solution proposed in this topic but this is very heavy for my RAM.

I found partially an other way to solve my problem. Inspired by this page and Kamil Naja's answer I wrote this code:

db.coll.find(
    { $where: 
        function() { return (new Set(this.field).size !== this.field.length)}
    }
)

It's more convenient to write, it's faster but it misses something particular for my problematic. I only want to count the duplicates of integer numbers.

For instance, here two arrays with different contents and both have duplicates but not from the same type:

  1. [1,2,3,4,5,6,6,7,8,'a','t'], array in field from file 1
  2. [1,2,3,4,5,6,7,8,'a','a'], array in field from file 2

With the current code above, it will select the two files, while I want the query to only return the file 1 because there are duplicates of integers.

How can I implement this condition, still using find() and not aggregate() ?

1 Answer 1

0

We can take into account only numbers with Array.filter() method.

db.coll.find({ $where: 
    function() { return (new Set(this.field.filter(x => typeof x === 'number')).size !== this.field.filter(x => typeof x === 'number').length)}
})
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answering so fast. Unfortunately it does not work and I don't know why because your code seems to be totally alright according to my researches. I tried without the strict equality: from === to ==. I tried with string as !== "string". I tried .filter(Number). All my attempts failed with Fetched 0 record(s) while I know these files do exist. I will search more.

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.