Is it possible to de-duplicate documents, comparing fields in an array and identify same data in more than a document

I’m looking to de-duplicate documents, with same data in “fields” array. Example document is given below.

“notes”: {
“_class”: “abclass”,
“createdBy”: “xyz”,
“createdDate”: 1521133343372,
“fields”: {
“accountNumber”: “accxyz”,
“comments”: “comments”,
“contactReason”: “reason”,
“ntId”: “aaa”,
“perm”: false,
“source”: “iii”
“id”: “ooooo”,
“typeRef”: “uuuuu”

We are using 4.6.3 Any quick suggestion would be appreciated, if couchbase is capable to do this at server level or should this be dealt with piece of code to compare.

Is object not array. If it is ARRAY you want eliminate duplicates do ARRAY_DISTINCT(fields)

Based on Above document the following query can give array of documents that have same fields.

SELECT  fields, ag  FROM default 
GROUP BY fields 

Thank you. I was trying to fetch the document Ids with duplicate data in fields array. The above n1ql works on smaller set of data, but not with millions of document. Alternatively, tried to retrieve the docIds using view, without any luck. Ref -
That resulted in ‘reduction too large’ errors. Any clue to do this other than from client side {code} to identify the duplicate documents. Final goal is to delete all the duplicates from couchbase.

thank you!

Do multiple queries by applying predicate of small ranges.