title: MongoError: Document failed validation - not sure why
body: <p>currently working on a basic CRUD application using MongoDB as my database, and keep getting the error <strong>MongoError: Document failed validation</strong> and can't understand how to fix it.</p>
  
  <p>my data seems to be posting from the front-end to the express server successfully as i can see user data in the request body via some console.log statements in my server-side code. </p>
  
  <p>heres what i've done so far, maybe someone can spot something i've overlooked:</p>
  
  <p><strong>front end:</strong> </p>
  
  <pre><code>// property values assigned by setter in useState hooks 
  const userDetails = {
      firstName: firstName,   // john
      lastName: lastName,     // doe
      email: email,           // johndoe111@gmail.com
      password: password,     // password1
    };
  
    const createUserAccount = () =&gt; {
      fetch("/api/users", {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(userDetails),
      })
        .then(console.log("POST request sucessful"))
        .catch((e) =&gt; console.log("error:", e.message));
    };
  </code></pre>
  
  <p><strong>request payload:</strong></p>
  
  <pre><code>{"firstName":"john","lastName":"doe","email":"johndoe111@gmail.com","password":"password1"}
  </code></pre>
  
  <p><strong>back-end:</strong></p>
  
  <pre><code>const jsonBodyParser = bodyParser.json();
  
  // some code to create an instance of mongo client 
  // some code to connnect to my cluster
  
  
  const createDatabase = async (client) =&gt; {
    const db = client.db(databaseName);
  
    try {
      await db.createCollection("users", {
        autoIndexId: true,
        validator: {
          $jsonSchema: {
            bsonType: "object",
            required: ["firstName", "lastName", "email", "password"],
            properties: {
              firstName: {
                bsonType: "string",
                description: "must be a string and is required",
              },
              lastName: {
                bsonType: "string",
                description: "must be a string and is required",
              },
              email: {
                bsonType: "string",
                description: "must be a string and is required",
              },
              password: {
                bsonType: "string",
                description: "must be a string and is required",
              },
            },
          },
        },
      });
      console.log("[MongoDB]: users collection created.");
      await db.createCollection("products", {});
      console.log("[MongoDB]: products collection created.");
    } catch (err) {
      console.log(err);
    }
  
    console.log("[MongoDB]: database created");
  };
  
  
  app.post("/api/users", jsonBodyParser, (req, res) =&gt; {
    console.log(req.body);
    const userDetails = {
      firstName: req.body.firstName,
      lastName: req.body.lastName,
      email: req.body.email,
      password: req.body.password,
    };
    console.log("user details: ", userDetails);
  
    try {
      client.db(databaseName).collection("users").insertOne({ userDetails });
    } catch (error) {
      console.log("[MongoDB]: ", error);
    }
  });
  </code></pre>
  
  <p><strong>request body (after being parsed):</strong></p>
  
  <pre><code>{
    firstName: 'john',
     lastName: 'doe',
     email: 'johndoe111@gmail.com',
    password: 'password1'
   }
  </code></pre>
  
  <p>many thanks!</p>