Getting Started with MongoDB usage (Windows) ①

Source: Internet
Author: User
Tags documentation mongodb documentation

Overview

We are certainly not unfamiliar with the database, there must be a lot of people used MySQL , but in MySQL the use of all kinds of tables, write the relationship between the table is very headache.

MongoDBIt's a database, but it's not a table, it's a collection of data, and I'm interested in how this data is stored. So I've compiled the entry-level usage for my own development, based on MongoDB3.6 's official documentation , which I MongoDB believe is useful for others.

This is a course on online MongoDB: MongoDB Introductory article

This is the official MongoDB documentation: the MongoDB Manual

What is MongoDB

Mongodbis the document oriented Databases, and it is also a " NoSQL database".

It's very easy to scale and fast.

The installation of MongoDB

1. Go to MongoDB's official website http://www.mongodb.org/downloads Download MSI installation package (CommunityServer version). The default path for installation is:C:\Program Files\MongoDB\Server\3.6\bin

2. In order to start MongoDB convenience, add the Mongod.exe path to the environment variable. environment variable, advanced system settings, properties, computer, add a default path to path:C:\Program Files\MongoDB\Server\3.6\bin

3. Create a new MongoDB folder on the D drive to place the data file and create the Data,logs folder under the MongoDB folder, and create the Mongodb.log file under the Logs folder.

4. Start cmd with the administrator and enter:mongod --dbpath D:\mongodb\data\ --logpath D:\mongodb\logs\mongodb.log --install --serviceName"MongoDB"

5. Start the MongoDB service with admin startup cmd, net start mongodb mongo 127.0.0.1:27017 enter MONGO database; net stop MongoDB Close MongoDB Service

Database operations

1. Create and access the database

Use database_name

Create a database named Test and enter the database, or directly into the database if the database already exists.

use TEST

2. Display the database.

Show DBS

Show all databases

show dbs

3. Deleting a database

Db.dropdatabase ()

Delete Test Database

use TESTdb.dropDatabase()
Collection operations

1. Create a Collection

Db.createcollection (name, options)

Create a database for the collection name Imooc

db.createCollection("imooc")

2. View the collection.

Show collections

View all Collections

show collections

3. Deleting a Collection

Db. Collection_name.drop ()

Delete Collection Imooc

db.imooc.drop()
Data manipulation

1.create operation

Db.collection.insertOne ()
Db.collection.insertMany ()
Db.collection.insert ()

Write a single and multiple pieces of data:

db.inventory.insertOne(   { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } })db.inventory.insertMany([   { item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },   { item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },   { item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }])

2.Read operation

Db.collection.find ()

Finds data with status "D" and displays 5.

db.inventory.find( { status: "D" } ).limit(5)

Finds data with status "D" and is displayed in a formatted format.

db.inventory.find( { status: "D" } ).pretty()

Look for data with status "A" or "D".

db.inventory.find( { status: { $in: [ "A", "D" ] } } )

Look for data with status "A" and qty of 30.

db.inventory.find( { status: "A", qty: 30} )

Look for data with status "A" or qty 30.

db.inventory.find( { $or: [ { status: "A" }, { qty:30 } ] } )

Find the second data with status "a".

db.inventory.find( { "status.1":  "A" } )

Finds the data in the Instock attribute that is qty 20. (The Instock property is a collection)

db.inventory.find( { 'instock.qty': 20 } )

Finds the first data in the Instock attribute that is qty 20. (The Instock property is a collection)

db.inventory.find( { 'instock.0.qty': 20 } )

Finds data with status ' A ' and returns only _id,item and status fields

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )

Finds data with status ' A ' and returns only the item field, not the status and _ID fields

db.inventory.find( { status: "A" }, { item: 1, status: 0, _id: 0 } )

Finds data with status "a" and returns only the _ID and item fields, as well as the UoM property of the Size field

db.inventory.find({ status: "A" }, { item: 1, "size.uom": 1 })

Finds data with item null or does not exist for Item property

db.inventory.find( { item: null } )

Finding data with the Item property null

db.inventory.find( { item : { $type: 10 } } )

To find data that does not exist for the Item property

db.inventory.find( { item : { $exists: false } } )

Equivalent to Db.users.find ({type:2}) because the result returns a loop pointer

var myCursor = db.users.find( { type: 2 } );myCursor

3.Update operation

Db.collection.updateOne ()
Db.collection.updateMany ()
Db.collection.replaceOne ()
Db.collection.update ()

Change the size.uom of the first data of item "paper" to "cm" and the status to "P"

db.inventory.updateOne(   { item: "paper" },   {     $set: { "size.uom": "cm", status: "P" },   })

Change the size.uom of all data with item "paper" to "cm" and status to "P"

db.inventory.updateMany(   { item: "paper" },   {     $set: { "size.uom": "cm", status: "P" },   })

Replace the first data with item "paper" with the last data

db.inventory.replaceOne(   { item: "paper" },   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 } ] })

4.delete operation

Db.collection.deleteOne ()
Db.collection.deleteMany ()
Db.collection.remove ()

Delete data for the first status "D"; Delete all data with status "D"

db.inventory.deleteOne( { status: "D" } )db.inventory.deleteMany( { status: "D" } )

Getting Started with MongoDB usage (Windows) ①

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.