So I have an application that uses MongoDB as a database. The application makes use of a few collections.

When and how should I go about defining the "schema" of the database which includes setting up all the collections as well as indexes needed?

AFAIK, you are unable to define empty collections in MongoDB (correct me if I am wrong, if I can do this it will basically answer this question). Should I insert a dummy value for each collection and use that to setup all my indexes?

What is the best practice for this?

Solution 1

You don't create collections in MongoDB.
You just start using them immediately whether they exist or not.

Now to defining the schema. As I said, you just start using a collection, so, if you need to ensure an index, just go ahead and do this. No collection creation. Any collection will effectively be created when you first modify it (creating an index counts).

> db.no_such_collection.getIndices()
[ ]
> db.no_such_collection.ensureIndex({whatever: 1})
> db.no_such_collection.getIndices()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "test.no_such_collection",
                "name" : "_id_"
        },
        {
                "v" : 1,
                "key" : {
                        "whatever" : 1
                },
                "ns" : "test.no_such_collection",
                "name" : "whatever_1"
        }
]

Solution 2

Create empty collection

This is how you could create empty collection in MongoDB using build in interactive terminal:
db.createCollection('someName'); // create empty collection

Just you don't really have to, because as someone pointed before, they will get created in real time once you start to interact with the database.

MongoDB is schema-less end of story, but ...

You could create your own class that interacts with mongo Database. In that class you could define rules that have to fulfilled before it can insert data to mongo collection, otherwise throw custom exception.

Or if you using node.js server-side you could install mongoose node package which allows you to interact with database in OOP style (Why bother to reinvent the wheel, right?).

Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.

docs: mongoose NPM installation and basic usage https://www.npmjs.com/package/mongoose mongoose full documentation http://mongoosejs.com

Mongoose use example (defining schema and inserting data)

var personSchema = new Schema({
    name: { type: String, default: 'anonymous' },
    age: { type: Number, min: 18, index: true },
    bio: { type: String, match: /[a-zA-Z ]/ },
    date: { type: Date, default: Date.now },
});

var personModel = mongoose.model('Person', personSchema);
var comment1 = new personModel({
    name: 'Witkor',
    age: '29',
    bio: 'Description',
});

comment1.save(function (err, comment) {
    if (err) console.log(err);
    else console.log('fallowing comment was saved:', comment);
});

Wrapping up ...

Being able to set schema along with restriction in our code doesn't change the fact that MongoDB itself is schema-less which in some scenarios is actually an advantage. This way if you ever decide to make changes to schema, but you don't bother about backward compatibility, just edit schema in your script, and you are done. This is the basic idea behind the MongoDB to be able to store different sets of data in each document with in the same collection. However, some restriction in code base logic are always desirable.

Solution 3

As of version 3.2, mongodb now provides schema validation at the collection level:

https://docs.mongodb.com/manual/core/schema-validation/

Solution 4

Example for create a schema :

db.createCollection("students", {
   validator: {
      $jsonSchema: {
         bsonType: "object",
         required: [ "name", "year", "major", "address" ],
         properties: {
            name: {
               bsonType: "string",
               description: "must be a string and is required"
            },
            year: {
               bsonType: "int",
               minimum: 2017,
               maximum: 3017,
               description: "must be an integer in [ 2017, 3017 ] and is required"
            },
            major: {
               enum: [ "Math", "English", "Computer Science", "History", null ],
               description: "can only be one of the enum values and is required"
            },
            gpa: {
               bsonType: [ "double" ],
               description: "must be a double if the field exists"
            },
            address: {
               bsonType: "object",
               required: [ "city" ],
               properties: {
                  street: {
                     bsonType: "string",
                     description: "must be a string if the field exists"
                  },
                  city: {
                     bsonType: "string",
                     description: "must be a string and is required"
                  }
               }
            }
         }
      }
   }
})

Solution 5

const mongoose = require("mongoose");
const RegisterSchema = mongoose.Schema({
  username: {
    type: String,
    unique: true,
    requied: true,
  },
  email: {
    type: String,
    unique: true,
    requied: true,
  },
  password: {
    type: String,
    requied: true,
  },
  date: {
    type: Date,
    default: Date.now,
  },
});

exports.module = Register = mongoose.model("Register", RegisterSchema);

I watched this tutorial.

Solution 6

You have already been taught that MongoDB is schemaless. However, in practice, we have a kind of "schema", and that is the object space of the object, whose relations a MongoDB database represents. With the ceveat that Ruby is my go-to language, and that I make no claims about exhaustiveness of this answer, I recommend to try two pieces of software:

1. ActiveRecord (part of Rails)
2. Mongoid (standalone MongoDB "schema", or rather, object persistence system in Ruby)

Expect a learning curve, though. I hope that others will point you to solutions in other great languages outside my expertise, such as Python.

Solution 7

1.Install mongoose: 
        npm install mongoose

2. Set-up connection string and call-backs

 // getting-started.js 

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');


//call-backs

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
  // we're connected!
});

3. Write your schema

var kittySchema = new mongoose.Schema({
  name: String
});

4. Model the schema

var Kitten = mongoose.model('Kitten', kittySchema);

5. Create a document

var silence = new Kitten({ name: 'Tom' });

console.log(silence.name); // Prints 'Tom' to console

// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
  var greeting = this.name
    ? "Meow name is " + this.name
    : "I don't have a name";
  console.log(greeting);
}

    enter code here

var Kitten = mongoose.model('Kitten', kittySchema);
Functions added to the methods property of a schema get compiled into the Model prototype and exposed on each document instance:

var fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak(); // "Meow name is fluffy"