Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does mongoose use schema when mongodb's benefit is supposed to be that it's schema-less?

I am a super newbie to mongodb. I am using mongoose to access mongodb from node.js, and know how to get things to work, but I don't think I understand why it works the way it does.

Most importantly, I don't understand why mongoose has 'schema' when one of the standout features of mongodb is that it doesn't have schema. Could someone enlighten me? thank you.

like image 607
Vlad Avatar asked Oct 28 '12 10:10

Vlad


People also ask

Why do we use schema when MongoDB is a schema less database Are there any benefits to having a schema for different collections?

What are the benefits of using a schemaless database? By operating without a schema, schemaless databases can store, retrieve, and query any data type — perfect for big data analytics and similar operations that are powered by unstructured data.

Why does Mongoose need a schema?

Mongoose Schema vs. Model. A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

Does Mongoose require schema?

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

Why do we use schema in MongoDB?

Most of the arguments for enforcing a schema on your data are well known: schemas maintain structure, giving a clear idea of what is going into the database, reducing preventable bugs and allowing for cleaner code (no more having to check the type of a field coming out of the database before using it).


1 Answers

Data without a schema is useless. You get a document from MongoDB, what do you do with it? Read some fields? You need to know the names, types and meanings of those fields. That’s a schema.

When people say that MongoDB “has no schema”, they really mean that it does not enforce schema the way SQL databases do. MongoDB pushes schema concerns up to your application level, where you can handle them more flexibly. For example, in order to add a new field to your documents, you don’t need to do an all-or-nothing ALTER on your collection—potentially millions of entries. You just add that field to your ODM (Mongoose) schema and you’re done.

like image 66
Vasiliy Faronov Avatar answered Sep 21 '22 16:09

Vasiliy Faronov