Can Atlas Search be used for exact substring search in a single-word field (like email)?

I have a collection with user data with emails. Is it possible to use MongoDB Atlas Search to search through them?

Example:
user1@test.com
anotheruser2@gmail.com
onemoreuser3@gmail.com

Searching for:
‘user’ - I want to get all three
‘gmail’ - last two
and so on.

Previously I was using regex, but it seems to start having issues with the number of documents in the database. Is there any better way to do this?

Hello @Mykhailo_Fomenko ,

Welcome to The MongoDB Community Forums! :wave:

MongoDB Atlas search provides a seamless and scalable solution for creating relevance-based application features through full-text search capabilities. It eliminates the necessity of running a separate search system alongside your database.

For your use-case, I added below 3 documents in my collection

[ {
  _id: ObjectId("64c759c53f1a52e9fd5053b8"),
  email: 'user1@test.com'
},
{
  _id: ObjectId("64c759da3f1a52e9fd5053b9"),
  email: 'anotheruser2@gmail.com'
},
{
  _id: ObjectId("64c759f33f1a52e9fd5053ba"),
  email: 'onemoreuser3@gmail.com'
} ]

Created a search index via Atlas UI, kept most settings as default but turned off Dynamic mapping and added a field mapping on my field ‘email’ with Data type as Autocomplete which performs a search for a word or phrase that contains a sequence of characters from an incomplete input string, below is my index definition

{
  "mappings": {
    "dynamic": false,
    "fields": {
      "email": {
        "tokenization": "nGram",
        "type": "autocomplete"
      }
    }
  }
}

Ran below query to check if any email field contains gmail

db.collection.aggregate( [   {
      $search: {
        autocomplete: {
          path: "email",
          query: "gmail",
        }
      }
    }])

Output

{
  _id: ObjectId("64c759da3f1a52e9fd5053b9"),
  email: 'anotheruser2@gmail.com'
}
{
  _id: ObjectId("64c759f33f1a52e9fd5053ba"),
  email: 'onemoreuser3@gmail.com'
}

Ran below query to check if any email field contains user

db.collection.aggregate( [   {
      $search: {
        autocomplete: {
          path: "email",
          query: "user",
        }
      }
    }])

Output

{
  _id: ObjectId("64c759da3f1a52e9fd5053b9"),
  email: 'anotheruser2@gmail.com'
}
{
  _id: ObjectId("64c759f33f1a52e9fd5053ba"),
  email: 'onemoreuser3@gmail.com'
}
{
  _id: ObjectId("64c759c53f1a52e9fd5053b8"),
  email: 'user1@test.com'
}

Please note that the above examples I have posted is based off your provided search terms and sample email strings. If you have any other examples / search terms that you believe the autocomplete operator may not work for then please let me know.

You may also wish to look at the following topic which may be of use to you too:

Let me know if this helps or if you have any more queries, would be happy to help you! :slightly_smiling_face:

Regards,
Tarun

A post was split to a new topic: How to search for substrings in any of the fields in the document?