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() ?

AvyWam
  • 890
  • 8
  • 28

1 Answers1

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)}
})
Valijon
  • 12,667
  • 4
  • 34
  • 67
  • 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. – AvyWam Apr 02 '20 at 00:31