MongoDB basic commands and operations

Source: Internet
Author: User
Tags mongodb client

Mongodb:nosql Database
    • Important indicator points in MongoDB
      • The three elements in MongoDB
        • Database
        • Collection
        • Document
      • The data store in MongoDB is stored in the form of Bson, and Bson is a binary json, so it looks like the recorded form is similar to the JSON data
      • The data in the collection in MongoDB is different from the data in the relational database, the MongoDB Chinese document structure can be different, so the extensibility is very good

MongoDB Benefits: Easy-to-scale, high-performance, flexible data model

The disadvantage of MongoDB: data duplication storage, occupy large space

MongoDB Boot
    • Command line (terminal) Start command

      • Mongod: Running the service side
      • MONGO: Starting the client
    • View Help commands, default port, log location

      • View Help: Mongod–help
      • To see if it started successfully: PS ajx|grep Mongod
      • Default side?: 27017
      • Location of the log:/var/log/mongodb/mongod.log
    • MongoDB Client
      • Start the local client: MONGO
      • View Help: Mongo–help
      • Exit: Exit or CTRL + C

MongoDB Basic CommandsManipulating Database Commands
    • View the current database: DB
    • View all databases: show Dbs/show databases
    • Switch database: Use db_name to delete the current database: Db.dropdatabase ()
      • Switch to a database that does not have, adding data is automatically created
    • Show current database status: Db.stats ()
    • Current database version: Db.version ()
    • View the current database link machine address: Db.getmongo ()
commands for manipulating collections
  • Do not create collections manually:
    • 向不存在的集合中第?次加?数据时, 集合会被创建出来
  • To create a combination manually:

    Create User: Db.createuser ({User: "Laowang", pwd: "123456", Roles:[{role: "Useradmin", DB: "Stu"}]})
    • Db.createcollection (name,options)
    • Db.createcollection ("Stu")
    • Db.createcollection ("Sub", {capped:true, size:10})
    • Parameter capped: The default value of false means that no upper bound is set and a value of TRUE indicates an upper limit
    • Parameter size: When the capped value is true, you need to specify this parameter, which indicates that the upper bound??, units of bytes
      • When the file reaches the upper limit, the previous data is overwritten, the earliest added data is moved out, the rest is moved up, and the last one is added
  • Show current so User: Show Users
  • Delete User: Db.removeuser ("UserName")
  • To view a collection:show collections

  • To delete a collection:db.集合名称.drop()

data types in the MONGO
  • Object ID:? file ID
  • String: Strings, most often, must be valid UTF-8
  • Boolean: Store A Boolean value, True or False
  • Integer: Integers can be either 32-bit or 64-bit, depending on the server
  • Double: Storing floating-point values
  • Arrays: An array or list in which multiple values are stored to a key
  • Object: What is the file of the embedded-type?
  • NULL: Store null value
  • Timestamp: Timestamp, representing the total number of seconds from 1970-1-1 to present
  • Date: The UNIX time format that stores the current period or time
Precautions :
    • Create a statement as follows: The format of the parameter is YYYY-MM-DD each file has a property, for _id, to guarantee the uniqueness of each file
      • new Date(‘2017-12-20‘)
    • OK?? Go to set _id plug?? File, if not provided, then MongoDB provides a unique _id for each file, type:objectID
    • Objectid is a 12-byte? Six binary number (understanding):
      • The first 4 bytes are the current timestamp
      • Next 3 bytes of machine ID
      • The next 2 bytes in MongoDB's service process ID
      • The last 3 bytes are simple increment values

Inserting Data
    • Db. Collection name. Insert (document) plug?? File, if you do not specify the _id parameter, MongoDB will assign the? Objectid type of _id
      • Db.stu.insert ({name: ' GJ ', gender:1})
      • Db.stu.insert ({_id: "20170101", Name: ' GJ ', gender:1})
    • Inserts a single specified as a dictionary, inserts multiple lines specified as a list
Save
    • Db. Collection name. Save (document)
    • If the _id of the file is already present, modify it if the _id of the file does not exist, then add
    • Different from: Insert if there is a direct error
Simple query:
    • db.集合名称.find()
Update
    • Grammar:db.集合名称.update(<query> ,<update>,{multi: <boolean>})
      • Parameter query: Query criteria
      • Parameter update: update operator
      • Parameter multi: optional, default is False, indicates that only the found record is updated, and a value of TRUE indicates that the full condition of the document is updated
    • For example: NOTE: "Multi update only works with $ operators" Update all, you must use$set
      • Db.stu.update ({name: ' hr '},{name: ' MNC '}) updates one, and fields that are not updated are discarded.
      • Db.stu.update ({name: ' hr '},{$set: {name: ' Hys '}}) update one
      • Db.stu.update ({},{$set: {gender:0}},{multi:true}) Update all
Delete
    • Grammar:db.集合名称.remove(<query>,{justOne: <boolean>})
      • parameter query: Optional, delete the condition of the document
      • Parameter justone: Optional, if set to TRUE or 1, delete only the bar, default false, means delete more than one

MongoDB basic commands and operations

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.