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
Activities
MongoDB Environment
Establish a MongoDB environment using one of the following:
Docker Playground
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
mongo
MongoDB in Docker
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
mongo
Install MongoDB
Install MongoDB on your own system.
- Review MongoDB: Tutorials
- Download and install MongoDB.
- Use the following terminal command to access the MongoDB command interface:
mongo
MongoDB Activities
Create a Collection
- 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
- Use the following MongoDB commands to query the countries collection in the temperature database:
db.countries.find()
Insert a Document
- 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
- 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
- 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
- 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
- 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
- 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
See Also
References
This article is issued from Wikiversity. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.