Install MongoDB Community on Windows using the Zip File

  1. Install MongoDB Community Edition

    • Download the MongoDB .zip file from the following link:
      MongoDB Download Center.

    • You can create a configuration file to specify options for MongoDB. To do so, create a file named mongod.cfg in the \bin\ directory within the directory where you extracted the MongoDB files (< install directory>\bin\mongod.cfg). For more information on creating a config file, see
      
          systemLog:
               destination: file
               path: "D:\\doc\\it\\mo\\bin\\mongod.log"
               logAppend: true
          storage:
               dbPath: "D:\\doc\\it\\mo\\db"
          net:
               bindIp: localhost
               port: 27017
      
                                  
    • Open a Windows command prompt/interpreter (cmd.exe) as an Administrator.

      
         mongod.exe --dbpath="D:\doc\it\mo\db"
      
                                  
      • The --dbpath option points to your database directory.

      To start MongoDB, run

      
        mongod.exe --config "D:\doc\it\mo\bin\mongod.cfg"  --install
      
                                  
  2. Install mongosh

    • Download the mongosh .zip file from the following link:
      MongoDB Download Center.
      To confirm that mongosh installed successfully, open a command prompt and run the following command:

      mongosh --version
                                  
      To connect to a MongoDB deployment running on localhost with default port 27017, run mongosh without any options:
      mongosh
                                  

  3. Create a MongoDB database

    • Use the show command to see all databases accessible in the cluster:

        mongosh> show dbs
        admin    40.00 KiB
        config   72.00 KiB
        local   112.00 KiB
        mongosh>
                                  
      Please note, admin and local are databases belonging to each MongoDB cluster. To create a database, you first need to switch the context to a non-existent database using the use command:
        mongosh> use com
        switched to db com
        com>
                                  
      To find the current database, type the db command:
        com> db
        com
        com>
                                  
  4. Create Documents

    • Inserting a document into a collection that doesn't exist creates that collection. This example inserts one document into the student collection. If the document does not specify _id, MongoDB adds one.

        db.student.insertOne({
          _id: 1,
         name: "王军",
         age: 20
        })
                                  
      insertOne() returns a document that includes the newly inserted document's _id field value.
      
        db.student.find( { name:"王军"} )
                                  
      insertMany() returns a document that includes the newly inserted documents _id field values.
      
         db.student.insertMany([
            {
              _id: 2,
              name: "张兵",
              age: 21
            },
            {
               _id: 3,
               name: "刘美",
               age: 26
           }
        ])