Despicable
Despicable

Reputation: 3957

Mongo db database design creation

It's my first experience with NoSQL. So I am very confused about its design creation. I read that MongoDB database contains Document , document contains Collection and collections contains Fields.
My Question is that If I want to create a DB and a table User and fields Username , password:
Collection = User
Fields = Username and password
Then what is the purpose of Document ??
Maybe this question is of a low standard but please consider it that it's my first experience with NoSQL.

Upvotes: 0

Views: 339

Answers (2)

user2418306
user2418306

Reputation: 2382

If you familiar with relational databases, you can think of collections as tables(relations), documents as rows(tuples), fields as columns(attributes). Difference is that fields(attributes) assotiated with documents(tuples) not collections(relations). Hence schemalessness as collection does not put any constrains on documents it hold. And documents can contain other documents as fields, which violates normalization and doesn't have much sense in relational model as there's no attributes stored with nested tuple. Consider Person document vs row counterpart:

{ lastname: 'Average', firstname: 'Joe', address: { country: 'USA', city: 'NY' } }

+----------+-----------+---------+
| lastname | firstname |  adress |
+----------+-----------+---------+
| Average  |    Joe    | USA, NY |
+----------+-----------+---------+

Upvotes: 0

Rafael R. S. Robles
Rafael R. S. Robles

Reputation: 897

For a MongoDB structure, the document would be User, and inside the document you could have Fields or even documents depending on the need. And a collection is a group of documents.

enter image description here

I recomend you to read JSON documentation and MongoDB Data Modeling.

Document: A record in a MongoDB collection and the basic unit of data in MongoDB. Documents are analogous to JSON objects but exist in the database in a more type-rich format known as BSON.

Collection: A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection have a similar or related purpose.

Upvotes: 2

Related Questions