Searching a specific domain name from URL records in MongoDB?

To search for a specific domain name from URL records in MongoDB, use the regex pattern with the /i flag for case-insensitive matching. This allows you to find URLs containing specific domain names regardless of their capitalization.

Syntax

db.collection.find({
    "fieldName": /domainName/i
});

Sample Data

Let us create a collection with URL documents ?

db.demo219.insertMany([
    {"details": {"WebsiteURL": "www.EXAMPLE.com"}},
    {"details": {"WebsiteURL": "www.gmail.com"}},
    {"details": {"WebsiteURL": "www.example.com"}}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e3e667203d395bdc2134718"),
        ObjectId("5e3e667803d395bdc2134719"),
        ObjectId("5e3e667f03d395bdc213471a")
    ]
}

Display all documents from the collection ?

db.demo219.find();
{ "_id" : ObjectId("5e3e667203d395bdc2134718"), "details" : { "WebsiteURL" : "www.EXAMPLE.com" } }
{ "_id" : ObjectId("5e3e667803d395bdc2134719"), "details" : { "WebsiteURL" : "www.gmail.com" } }
{ "_id" : ObjectId("5e3e667f03d395bdc213471a"), "details" : { "WebsiteURL" : "www.example.com" } }

Example: Search for "example" Domain

Search for URLs containing "example" domain name (case-insensitive) ?

db.demo219.find({"details.WebsiteURL": /example/i});
{ "_id" : ObjectId("5e3e667203d395bdc2134718"), "details" : { "WebsiteURL" : "www.EXAMPLE.com" } }
{ "_id" : ObjectId("5e3e667f03d395bdc213471a"), "details" : { "WebsiteURL" : "www.example.com" } }

Key Points

  • The /i flag makes the search case-insensitive, matching both "EXAMPLE" and "example".
  • Use dot notation "details.WebsiteURL" to search within nested document fields.
  • Regex patterns match partial strings within the URL field.

Conclusion

Use regex with the /i flag to perform case-insensitive domain searches in MongoDB URL records. This method efficiently finds all documents containing the specified domain name regardless of capitalization.

Updated on: 2026-03-15T01:58:56+05:30

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements