Document
MongoDB store data as documents.
Introduction
A document is a data structure composed of field and value pairs. The value of a field can be a simple data type, such as a string or a number, or it can be a more complex data type, such as an array or another document.
Below you have an example of a JSON document that describes the employee Laura:
"_id": "laura",
"firstName": "Laura",
"lastName": "Codina",
"email": "laura@acme.com",
"department": "Finance"
}Documents in document databases are self-describing, which means they contain both data values and information about what type of data is stored.
Below you have a document from another employee who works in several departments:
"_id": "roser",
"firstName": "Roser",
"lastName": "Vilalta",
"email": "roser@acme.com",
"salary": 70000,
"department":
}This second document has some differences compared to the first example.
In document databases:
- Documents not only describe themselves, but also their schema is dynamic, which means you don’t need to define it before starting to save data.
- Fields can differ between different documents in the same database, and you can modify the document structure at will, adding or removing fields as you go.
- Documents can also be nested, that is, a field of a document can have a value consisting of another document, allowing you to store complex data within a single document entry.
"_id": "david",
"firstName": "David",
"lastName": "de Mingo",
"email": "david@acme.com",
"department": ,
"socialMediaAccounts": "type": "facebook",
"username": "david_de_mingo"
},
"type": "twitter",
"username": "@ddemingo"
}
]
}WSL
Start a new Windows Subsystem for Linux (WSL) terminal.
wslEstàs llegint una vista prèvia.
Inicia sessió per llegir l'article complet. Qualsevol compte obre 4 articles gratuïts al mes; l'alumnat i el professorat llegeixen les pàgines del seu curs sense límit.
Inicia sessió