简体   繁体   中英

regex in mongo collection aggregate query

I am working in mongodb. I have a collection, inside the collection, there is an EmailAddress field. In EmailAddress there is numerous type of invalid & valid EmailAddress data contain.

I have written a query to find the duplicate record as well I am also able to find valid EmailAddress but not able to merge both queries.

db.EZShredCustomerData.aggregate(
   {"$match": {"AccountObjectId" : "597ee5ed1f2632885bb650"} },
   {"$group" : 
        {
            "_id": "$EmailAddress",
            "AccountObjectID" : { $first: '$AccountObjectId' }, 
            "EmailAddress" : { $first: '$EmailAddress' },
            "count": { "$sum": 1 } 

   } },
{"$match": {"count" : { $gte: 1 }} })

Through this query, I am getting duplicate records Like

rdod@drivedag.com   2
n/a                24
entbill@entaac.org 12
none               16
;                   8
                   460

I also write who validate email is valid or not

db.EZShredCustomerData.find({ 
  "AccountObjectId": "597ee5ed1f2632885bb650", 
  EmailAddress: { $regex: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/}
  })

Problem is that, due to lack of experience in mongo not able to merge both queries and get expected data

AccountObjectID          EmailAddress
597ee5ed1f2632885bb650  rdod@drivedag.com   
597ee5ed1f2632885bb650  entbill@entaac.org 

I have done by myself following is code.

db.EZShredCustomerData.aggregate(
   {"$match": {"AccountObjectId" : "597ee5ed1f2632885bb650"} },
   {"$group" : 
        {
            "_id": "$EmailAddress",
            "AccountObjectID" : { $first: '$AccountObjectId' }, 
            "EmailAddress" : { $first: '$EmailAddress' },
            "count": { "$sum": 1 } 

   } },
{"$match": {"count" : { $eq: 1 }} },
{"$match": {"EmailAddress" : { $regex: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ }} })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM