'Mongodb find with encrypted data

What I exactly need is decrypt email that already stored in mongodb and find exact matching document.. What I am doing is login the user... Let's see what is my code :-

My MongoDB collection :-

    {
    "_id" : ObjectId("608999124116b73cd4b3378d"),
    "firstName" : "U2FsdGVKHNnkmnUJBkjWphggtmRhMfhh/Fsz0uhzA=",
    "lastName" : "U2FsdGVkXJHBhbdHgjbJHW3Xi8YWKymfJYme6CZE=",
    "email" : "U2FsJHbgvJHbyhjbghbgfhkjnbKymfJYme6CZE=",
    "password" : "$2a$10$d3aV7oMRIs9lU7dBh3tNR.u5KduY95LkjrrX8l4GINKr4XHqFREb2",
    "userId" : "1caae2a9-eda6-49f7-8a22-40c7fa3c8920"
    },
    {
    "_id" : ObjectId("608999124116b73cd4b3378d"),
    "firstName" : "U2FsdGVKHNnkmnUJBkjWphggtmRhMfhh/Fsz0uhzA=",
    "lastName" : "U2FsdGVkXJHBhbdHgjbJHW3Xi8YWKymfJYme6CZE=",
    "email" : "U2FsJHbgvJHbyhjbghbgfhkjnbKymfJYme6CZE=",
    "password" : "$2a$10$d3aV7oMRIs9lU7dBh3tNR.u5KduY95LkjrrX8l4GINKr4XHqFREb2",
    "userId" : "1caae2a9-eda6-49f7-8a22-40c7fa3c8920"
    },
    

In server side nodejs :-

var userTyped = req.body();
// userTyped = { "email":"[email protected]","password":"SOMETHING" }
var userData = await db.collection('users').findOne({email:userTyped.email});// Maybe I need to change something in here
sendToClientSide(userData);// Finded data send to user

I have a function that returns decrypted data

    decryptData(data){
          // some code here 
          return decryptedData;
     }

I need to find the user data from database but the stored data is encrypted so I need to decrypt all users data in database and find the best matching data... How to get exact user data without decrypting all users data..?

The problem with the current code is email comparison isn't working that I mentioned in nodejs...



Solution 1:[1]

Searching inside compressed data is not possible without uncompressing.

In this case, I recommend doing the opposite, write a function that encrypts the input data using the same method as the one used to encrypt the data in your mongo.

Encrypt the email with that method, or any other unique value provided, and do a find_one() for that value in your collection.

This will also work best if you add an index to that field, making queries faster.

Solution 2:[2]

It's quite late to respond but I did research on the same thing and Mongo has come up with Field Level Encryption that makes things really easy. There are also NPM libraries that can help you with it instead of setting up individual functions for everything. Maybe this link will help you.

https://www.smallcase.com/blog/fle-mongodb-community-node-mongoose/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yael Araizaga
Solution 2 Akshay Balwani