2 / 2
Oct 2023

I need to get the user object from the db without the password field

let filter_options = doc!{"_id": id}; let projection = doc! {"password": 0}; let opts = FindOneOptions::builder().projection(projection).build(); let user = self .collection .find_one(filter_options, opts) .await .unwrap();

I am getting the following error
Error { kind: BsonDeserialization(DeserializationError { message: "missing field password" })}

In the shell all work correctly
db.user.findOne({_id: ObjectId('652b9ae383480f1ae820d6d5')}, {password: 0})

Maybe you need to add some $ifNull: clause to your projection, if that’s possible in Swift.