MongoDB Quick Start Guide and docker-compose Quick Experience

Advantages of MongoDB over RDBMS

  1. Less Schemas - MongoDB is a document database where a collection contains different documents.The number of fields, content, and document size may vary between documents.
  2. The structure of a single object is clear.
  3. There are no complex joins.
  4. In-depth query capabilities.MongoDB supports dynamic querying of documents using a document-based query language almost as powerful as SQL.
  5. SQL Tuning (Optimized)
  6. Easy to expand
  7. There is no need to convert/map application objects to database objects.
  8. Use internal memory to store (window-style) working sets so that data can be accessed more quickly

RDBMS: Relational Database Management System

Why use MongoDB

  1. Document-oriented storage, BSON format storage, or Binary JSON
  2. Single-key index, composite index, multikey index, geospatial index, full-text index, and hash index
  3. MongoDB implements highly available master-slave replication 
  4. Auto-slicing auto sharding 
  5. Rich queries 
  6. Fast in-place updates: Most update operations do not require new space
  7. Professional support for MongoDB map/reduce support
  8. Gridfs: Cluster file support of various size sizes

Where to use MongoDB

  1. Big data
  2. Content management and delivery
  3. Mobile and Social Infrastructure
  4. User data management
  5. Data Center

Doker-compose Quick Start MongoDB


version: '3'
        image: mongo:4.2.6 # Mirror: Version
        container_name: mongo_db
          - MONGO_INITDB_DATABASE=Default database
          - MONGO_INITDB_ROOT_USERNAME=Your root Administrator Name
          - MONGO_INITDB_ROOT_PASSWORD=Your root Administrator name password
          - ./mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
          - ./mongo/mongo-volume:/data/db
          - "27017-27019:27017-27019"
        restart: always


// db.getSiblingDB() is equivalent to use admin;
        user: 'user',
        pwd: 'user',
        roles: ['readWrite']

Then execute the command

docker-compose up -d

Navicat Premium Connection

Fill in the parameters for docker-compose.yml above
Test connection, test successful

If you don't show it, go

navicat == Menu== Show== Show hidden items on the hook

Restart navicat

Open a collection to view in the lower right corner

  • Grid View
  • Tree View
  • JSON View


1. Create Action

  • db.collection.insertOne()
  • db.collection.insertMany()

// Insert a collection named products and insert data (new if no collection exists)
    item: "card",
    qty: 15

// Bulk Insert Data
    item: "card1",
    qty: 16
}, {
    item: "card2",
    qty: 17
}, {
    item: "envelope",
    qty: 20
}, {
    item: "stamps",
    qty: 30

2. Query operation

  • db.collection.find()

db.products.find({ qty: 15 }).limit(1)

3. Update operation

  • db.collection.updateOne()


Find the first document that matches the filter and apply the specified update changes.

    item: "stamps"

// Find item as stamps and modify its qty to 60
        "item": "stamps"
        $set: {
            "qty": 60

    item: "stamps"
  • db.collection.updateMany()

// Modify qty greater than 15 to 15
        qty: {
            $gt: 15
        $set: {
            "qty": 15

  • db.collection.replaceOne()

Replace the first matching document in the collection that matches the filter with a replacement document.

// Replace item in collection products with stamps as a whole 
    { "item" : "stamps" },
    { "item" : "stampss", "Borough" : "Manhattan" }

4. Delete operation

 // Delete item s that are stampss
 db.products.deleteOne( { "item" : "stampss" } );
 // Batch deletion of qty less than 15
 db.products.deleteMany( { "qty" : {$lt: 15} } );

Continuous update of connection 

Tags: Java MongoDB Database Docker Navicat

Posted on Thu, 14 May 2020 12:58:34 -0400 by thinkgfx