Hello all
Today, we talk about Logical Structures in MongoDB and how do you insert one or many register into a collection.
But first, we need to understand what a database is, a collection, a document and a field in MongoDB.
For example, one database can have many collections. A collection can have many documents, and documents can have fields. It's easy :)
As the glossary definitions:
Database
A physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.
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.
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.
A physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.
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.
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.
Documents have the following restrictions on field names:
- The field name _id is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array. If the _id contains subfields, the subfield names cannot begin with a ($) symbol.
- Field names cannot contain the null character.
- The server permits storage of field names that contain dots (.) and dollar signs ($). MongoDB 5.0 adds improved support for the use of ($) and (.) in field names. There are some restrictions.
A name-value pair in a document. A document has zero or more fields. Fields are analogous to columns in relational databases.
Ok, it's easy. Now, we'll create a new database, a new collection and insert the first documents on the collection. Of course, we'll list the documents.
View all databases
show dbs
show databases
Ok, it's easy. Now, we'll create a new database, a new collection and insert the first documents on the collection. Of course, we'll list the documents.
View all databases
show dbs
show databases
Create new database or switch database
use thordb
View current Database
db
Show all collections in database
show collections
Create collection call movies
db.createCollection('movies')
As soon as possible, we'll have a post just talking about Create Collections.
Insert a single row
db.movies.insertOne({codigo: 1, trilogy: "Harry Potter", sequence: "The Philosophers Stone", year: "2001"})
Insert many rows
db.movies.insertMany ([
{codigo: 2, trilogy: "Harry Potter", sequence: "Harry Potter and the Chamber of Secrets", year: "2002"},
{codigo: 3, trilogy: "Harry Potter", sequence: "Harry Potter and the Prisoner of Azkaban", year: "2004"},
{codigo: 4, trilogy: "Harry Potter", sequence: "Harry Potter and the Goblet of Fire", year: "2005"},
{codigo: 5, trilogy: "Harry Potter", sequence: "Harry Potter and the Order of the Phoenix", year: "2007"},
{codigo: 6, trilogy: "Transformers", sequence: "Transformers: Dark of the Moon", year: "2011"},
{codigo: 7, trilogy: "Transformers", sequence: "Transformers: Age of Extinction", year: "2014"},
{codigo: 8, trilogy: "Transformers", sequence: "Transformers: The Last Knight", year: "2017"},
{codigo: 9, trilogy: "Harry Potter", sequence: "Harry Potter and the Half-Blood Prince", year: "2009"},
{codigo: 10, trilogy: "Harry Potter", sequence: "Harry Potter and the Deathly Hallows P1", year: "2010"},
{codigo: 11, trilogy: "Transformers", sequence: "Transformers: Revenge of the Fallen", year: "2009"},
{codigo: 12, trilogy: "Harry Potter", sequence: "Harry Potter and the Deathly Hallows P2", year: "2011"},
{codigo: 13, trilogy: "Transformers", sequence: "Transformers", year: "2007"},
{codigo: 14, trilogy: "Transformers", sequence: "Bumblebee", year: "2018"},
{codigo: 15, trilogy: "Harry Potter", sequence: "Fantastic Beasts and Where to Find Them", year: "2016"}
{codigo: 3, trilogy: "Harry Potter", sequence: "Harry Potter and the Prisoner of Azkaban", year: "2004"},
{codigo: 4, trilogy: "Harry Potter", sequence: "Harry Potter and the Goblet of Fire", year: "2005"},
{codigo: 5, trilogy: "Harry Potter", sequence: "Harry Potter and the Order of the Phoenix", year: "2007"},
{codigo: 6, trilogy: "Transformers", sequence: "Transformers: Dark of the Moon", year: "2011"},
{codigo: 7, trilogy: "Transformers", sequence: "Transformers: Age of Extinction", year: "2014"},
{codigo: 8, trilogy: "Transformers", sequence: "Transformers: The Last Knight", year: "2017"},
{codigo: 9, trilogy: "Harry Potter", sequence: "Harry Potter and the Half-Blood Prince", year: "2009"},
{codigo: 10, trilogy: "Harry Potter", sequence: "Harry Potter and the Deathly Hallows P1", year: "2010"},
{codigo: 11, trilogy: "Transformers", sequence: "Transformers: Revenge of the Fallen", year: "2009"},
{codigo: 12, trilogy: "Harry Potter", sequence: "Harry Potter and the Deathly Hallows P2", year: "2011"},
{codigo: 13, trilogy: "Transformers", sequence: "Transformers", year: "2007"},
{codigo: 14, trilogy: "Transformers", sequence: "Bumblebee", year: "2018"},
{codigo: 15, trilogy: "Harry Potter", sequence: "Fantastic Beasts and Where to Find Them", year: "2016"}
Show all rows in a collection
db.movies.find()
Yes baby, today you learned the basic about the "C" or "Create Operations" from CRUD in MongoDB.
That's it. As I said, it was easy. The basic always is easy!!!
On next posts, we'll learn about the "R" or "Read Operations" and we'll learn about array operations.
I hope that it helps you.
Regards
Mario
Previous articles:
Nenhum comentário:
Postar um comentário
Isso te ajudou? Comente...