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?
Overview. You can create a schema for your App in one of two ways: Create a Realm Object Model from an Atlas App Services Schema: If you have data in your MongoDB Atlas cluster already, MongoDB generates a schema by sampling your data.
We can get the schema object/first document of the collection using : var schemaObj = db. users. findOne();
A flexible data model, such as the one found in MongoDB, lets you store or aggregate any type of data and dynamically change schema without application downtime. Data in MongoDB is stored in documents and similarly structured documents are typically organized into collections.
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" } ]
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.
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
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With