MongoDB
Appearance
MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).[1]
Readings
[edit | edit source]Activities
[edit | edit source]MongoDB Environment
[edit | edit source]Establish a MongoDB environment using one of the following:
Docker Playground
[edit | edit source]Docker Playground is a free online Docker environment. It requires no installation or configuration.
- Use Play with Docker. Create an account and/or log in.
- Start an interactive session and add a new instance.
- In the terminal window, enter the following commands:
docker run -d --name=mongo-server mongo
docker exec -it mongo-server bash
mongosh
MongoDB in Docker
[edit | edit source]You can use your own Docker environment to run MongoDB.
- Install Docker Desktop or the Docker Engine.
- In a terminal window, run the following commands:
docker run -d --name=mongo-server mongo
docker exec -it mongo-server bash
mongosh
Install MongoDB
[edit | edit source]Install MongoDB on your own system.
- Review MongoDB: Tutorials
- Download and install MongoDB.
- Use the following terminal command to access the MongoDB command interface:
mongosh
MongoDB Activities
[edit | edit source]Create a Collection
[edit | edit source]- Use the following MongoDB commands to create a temperature database and document collection:
use temperature
db.countries.insertMany( [
{ country: "Bulgaria", temperature: "45.2 °C" },
{ country: "Canada", temperature: "45 °C" },
{ country: "Federated States of Micronesia", temperature: "36.1 °C" },
{ country: "Finland", temperature: "37.2 °C" },
{ country: "Germany", temperature: "40.3 °C" },
{ country: "Japan", temperature: "41 °C" },
{ country: "Marshall Islands", temperature: "35.6 °C" },
{ country: "Palau", temperature: "35 °C" },
{ country: "Turkmenistan", temperature: "50.1 °C" },
] );
- Use the following MongoDB command to show existing databases:
show databases
- Use the following MongoDB command to show existing collections:
show collections
Query a MongoDB Database
[edit | edit source]- Use the following MongoDB commands to query the countries collection in the temperature database:
db.countries.find()
Insert a Document
[edit | edit source]- Use the following MongoDB command to insert a temperature document:
db.countries.insertOne( { country: "United States of America", temperature: "56.7 °C" } )
- Use the following MongoDB command to display the inserted document:
db.countries.find( {country: "United States of America"} )
Update a Document
[edit | edit source]- Use the following MongoDB command to update a temperature document:
db.countries.update(
{ country: "United States of America" },
{ $set: { temperature: "56.5 °C" } }
)
- Use the following MongoDB command to display the inserted document:
db.countries.find( {country: "United States of America"} )
Remove a Document
[edit | edit source]- Use the following MongoDB command to remove a temperature document:
db.countries.remove( { country: "United States of America" } )
- Use the following MongoDB command to display the remaining documents:
db.countries.find()
Remove All Documents
[edit | edit source]- Use the following MongoDB command to remove a temperature document:
db.countries.remove({})
- Use the following MongoDB command to verify there are no remaining documents:
db.countries.find()
Drop a Collection
[edit | edit source]- Use the following MongoDB command to show existing collections:
show collections
- Use the following MongoDB command to remove the countries collection:
db.countries.drop()
- Use the following MongoDB command to show existing collections:
show collections
Drop a Database
[edit | edit source]- Use the following MongoDB command to show existing databases:
show databases
- Use the following MongoDB command to remove the temperature database:
db.dropDatabase()
- Use the following MongoDB command to show existing databases:
show databases