Member-only story

Using MongoDB with Mongoose — String, Number, and Date Schema Types

John Au-Yeung
3 min readJan 14, 2021

--

Photo by Dušan Smetana on Unsplash

To make MongoDB database manipulation easy, we can use the Mongoose NPM package to make working with MongoDB databases easier.

In this article, we’ll look at how to use Mongoose to manipulate our MongoDB database.

String Schema Types

We can set various properties for strings schema types.

They include:

  • lowercase: boolean, whether to always call .toLowerCase() on the value
  • uppercase: boolean, whether to always call .toUpperCase() on the value
  • trim: boolean, whether to always call .trim() on the value
  • match: RegExp, creates a validator that checks if the value matches the given regular expression
  • enum: Array, creates a validator that checks if the value is in the given array.
  • minlength: Number, creates a validator that checks if the value length is not less than the given number
  • maxlength: Number, creates a validator that checks if the value length is not greater than the given number

For example, we can write:

const mongoose = require('mongoose');
const connection = "mongodb://localhost:27017/test"…

--

--

No responses yet