mongoose schema types

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

MongoDB is a schema-less database. It is one of the reasons we use mongoose with it. Mongoose is an Object Modelling Library(ODM). Apart from providing schema, mongoose also provides Validation. Other features of mongoose are plugins, middleware, and population. MongoDB stores data in BSON. BSON in binary encoded JSON. It provides more data types than JSON. In this article, we will discuss all the mongoose schema types.

Mongoose schema types

We can say that mongoose schema is the configuration object for a Mongoose model. Then, we can also say, a SchemaType is a configuration object for an individual property. Well, this is after all the aim of defining a schema. Say, we want to store the names of all the employees in a collection. The name of anything is always a string, not a number, or a boolean value. So its schema type should be a string so that no one could store anything inappropriate. We have to define the schema type of each property so no bad data could be inserted in the collection. Let’s see what are the SchemaTypes that are supported by mongoose.

String

The string is one of the most commonly used data types in the programming world along with numbers. A string in programming is a sequence of characters written between single or double-quotes. Anything written between single or double quotes is a string.

1
2
3
4
var schema = new Schema({
                name: String
               
            })

Number

Another most commonly used data type in the programming world is Number. It can vary in programming languages. For example in Java, we have different types of data types to store numbers, such as int, short, double. Because mongoose is used in Javascript, we simply use Number.

1
2
3
4
5
var schema = new Schema({
                name : String,
                age : Number
               
            })

Date

The Date data type is used to store dates.

1
2
3
4
5
6
var schema = new Schema({
                name : String,
                age : Number,
                dateOfBirth: Date
               
            })

ObjectIds

The ObjectIds is a special data type that is for storing unique identifies. It is declared in the following way.

1
2
3
4
5
6
7
var schema = new Schema({
                name : String,
                age : Number,
                dateOfBirth: Date,
                Id: mongoose.ObjectIds
               
            })

Boolean

In mongoose, Boolean is plain javascript booleans. The following values can be cast for true:

  1. true
  2. ‘true’
  3. 1
  4. ‘1’
  5. ‘yes’

The following values can be cast for false:

  1. false
  2. ‘false’
  3. 0
  4. ‘0’
  5. ‘no’
1
2
3
4
5
6
7
var schema = new Schema({
                name : String,
                age : Number,
                dateOfBirth: Date,
                Id: mongoose.ObjectIds
                onSite: true
            })

Arrays

The array is one of the most commonly used as well as one of the most useful data structures. It is also supported by the mongoose. While declaring an array in the schema, we need to put the data type in the square brackets.

1
2
3
4
5
6
7
8
var schema = new Schema({
                name : String,
                age : Number,
                dateOfBirth: Date,
                Id: mongoose.ObjectIds
                onSite: true,
                locations: [String]
            })

Buffer

We can also declare a path as Buffer in mongoose.

1
2
3
4
5
6
7
8
9
var schema = new Schema({
                name : String,
                age : Number,
                dateOfBirth: Date,
                Id: mongoose.ObjectIds
                onSite: true,
                locations: [String],
                Data: Buffer
            })

Mixed

The Mixed schema type means “anything goes”. The property that has a mixed schema type can hold any value. The following values can be cast for a mixed type:

  1. {}
  2. Object
  3. Schema.Types.Mixed
  4. mongoose.Mixed
1
2
3
4
5
6
7
8
9
10
var schema = new Schema({
                name : String,
                age : Number,
                dateOfBirth: Date,
                Id: mongoose.ObjectIds
                onSite: true,
                locations: [String],
                Data: Buffer,
                others : Schema.Types.Mixed
            })

Maps

The map was added to mongoose 5.1.0. It is a subclass of Javascript’s Map class. Maps in mongoose are used to create nested documents with arbitrary keys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var schema = new Schema({
                name : String,
                age : Number,
                dateOfBirth: Date,
                Id: mongoose.ObjectIds
                onSite: true,
                locations: [String],
                Data: Buffer,
                others : Schema.Types.Mixed,
                supervisorOf : {
                    type: map
                    of:  String
                }
            })

Observe the “supervisorOf” property. Its type is a map but it also one more field – of. This field defines what type of values will this map holds.

Conclusion

These were all the schema types supported by the mongoose.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var schema = new Schema({
                name : String, //String
                age : Number, //Number
                dateOfBirth: Date, //Date
                Id: mongoose.ObjectIds //ObjectIds
                onSite: true, //Boolean
                locations: [String], //Array
                Data: Buffer, //Buffer
                others : Schema.Types.Mixed, //Mixed
                supervisorOf : {
                    type: map //map
                    of:  String
                }
            })

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.