MongoDB query returns null even when data exists

Viewed 43

I've got some class

public class User

    {
        [BsonId] //id
        public Guid Id { get; private set; }
        [BsonElement("FirstName")]
        public string FirstName { get; }
        [BsonElement("LastName")]
        public string LastName { get; }
        [BsonElement("Email")]
        public string Email { get; }
        [BsonElement("Password")]
        public string Password { get; }

        

And a query to check if user wrote correct password

public bool PairEmailAndPass(string email, string password)
        {
            var collection = db.GetCollection<User>("Users");
            var filter = Builders<User>.Filter.Eq("Email", email);
            var record = collection.Find(filter).FirstOrDefault();
            if (record.Password == password) return true;
            else return false;
        }

And it always returns false. I made a unit test to see what is the value of record.Password, and I noticed, that the query returns an empty object. What's wrong?

That's the function that inserts objects into the database

public void AddNewUser(User user)
        {
            var collection = db.GetCollection<User>("Users");
            collection.InsertOne(user);
        }
0 Answers
Related