Categories
  Encyclosphere.org ENCYCLOREADER
  supported by EncyclosphereKSF

MongoDB

From Wikiversity - Reading time: 3 min

MongoDB logo
MongoDB logo

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]
  1. Wikipedia: MongoDB
  2. Wikipedia: Document-oriented database

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.

  1. Use Play with Docker. Create an account and/or log in.
  2. Start an interactive session and add a new instance.
  3. 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.

  1. Install Docker Desktop or the Docker Engine.
  2. 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.

  1. Review MongoDB: Tutorials
  2. Download and install MongoDB.
  3. Use the following terminal command to access the MongoDB command interface:
    • mongosh

MongoDB Activities

[edit | edit source]

Create a Collection

[edit | edit source]
  1. 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" },
    ] );
  2. Use the following MongoDB command to show existing databases:
    show databases
  3. Use the following MongoDB command to show existing collections:
    show collections

Query a MongoDB Database

[edit | edit source]
  1. Use the following MongoDB commands to query the countries collection in the temperature database:
    db.countries.find()

Insert a Document

[edit | edit source]
  1. Use the following MongoDB command to insert a temperature document:
    db.countries.insertOne( { country: "United States of America", temperature: "56.7 °C" } )
  2. Use the following MongoDB command to display the inserted document:
    db.countries.find( {country: "United States of America"} )

Update a Document

[edit | edit source]
  1. Use the following MongoDB command to update a temperature document:
    db.countries.update(
        { country: "United States of America" },
        { $set: { temperature: "56.5 °C" } }
    )
  2. Use the following MongoDB command to display the inserted document:
    db.countries.find( {country: "United States of America"} )

Remove a Document

[edit | edit source]
  1. Use the following MongoDB command to remove a temperature document:
    db.countries.remove( { country: "United States of America" } )
  2. Use the following MongoDB command to display the remaining documents:
    db.countries.find()

Remove All Documents

[edit | edit source]
  1. Use the following MongoDB command to remove a temperature document:
    db.countries.remove({})
  2. Use the following MongoDB command to verify there are no remaining documents:
    db.countries.find()

Drop a Collection

[edit | edit source]
  1. Use the following MongoDB command to show existing collections:
    show collections
  2. Use the following MongoDB command to remove the countries collection:
    db.countries.drop()
  3. Use the following MongoDB command to show existing collections:
    show collections

Drop a Database

[edit | edit source]
  1. Use the following MongoDB command to show existing databases:
    show databases
  2. Use the following MongoDB command to remove the temperature database:
    db.dropDatabase()
  3. Use the following MongoDB command to show existing databases:
    show databases

See Also

[edit | edit source]

References

[edit | edit source]

Licensed under CC BY-SA 3.0 | Source: https://en.wikiversity.org/wiki/MongoDB
11 views | Status: cached on November 21 2024 18:56:16
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF