MongoDB- commonly used commands

Introduction MongoDB

MongoDB is a distributed file storage based database. By the C ++ written language. Designed to provide scalable, high-performance data storage solution for WEB applications.
MongoDB is between a relational database product and between non-relational databases, non-relational database functions among the richest and most like a relational database. It supports data structure is very loose, is similar json of bson format, it is possible to store more complex data types. Mongo biggest feature is that it supports very powerful query language, its syntax is somewhat similar to the object-oriented query language, most of the functionality can be achieved almost single-table queries similar to a relational database, but also support for the establishment of a data index .

Feature

It is characterized by high performance, easy to deploy, easy to use, very convenient to store data. The main features are:
* Set is stored for easy storage of data object type.
mongodb cluster reference
* Free mode.
* Support for dynamic queries .
* Supports fully indexed, it contains internal objects.
* Support queries.
* Support replication and failover.
* Using an efficient binary data storage, including large objects (e.g., video, etc.).
* Automatic processing chips to support the expansion of the cloud level.
* Support RUBY , PYTHON , JAVA , C ++ , PHP , C # and other languages.
* File storage format is BSON (one kind of JSON extension).
* Through network access.

Data Model

A MongoDB instance can contain a set of databases, one can DataBase contains a set of Collection (collection), a collection can contain a set of Document (document). Document contains a set of a Field (fields), each field is a key / value pair.
key: you must be a string type.
value: it may comprise the following types.
  • Type basic types, e.g., string, int, float, timestamp, binary like.
  • A document.
  • Array type.

Common Commands

Basic common commands

# View the current use of the database 
db
 # to view the current all databases on the server disk 
Show Databases
 # switch databases currently in use, the library does not exist on behalf of created library 
use dbname
 # view all current database table on disk 
Show the Tables
 # using the current tablename table in the database to create a table in memory 
db.tablename

CRUD

increase

Use the object does not exist, create objects on behalf of,

# Create the database and using the database 
use S20
 # Create a table 
db.stutent
 # add data to the table is not recommended to use [the official] 
db.stutent.insert ({name: " henry " }) # add a data 
db.stutent.insert ([{name: " henry " }, {name: " Egon " }]) # add multiple data 
# [recommended] official 
db.stutent.instrtOne ({name: " henry " })   # add singled data 
db. stutent.insertMany ([{name: " whirlpools " }, {name: " 500ml " }])

check

# View all data 
db.tablename.find ()
 # view all data in line with the conditions of 
db.tablename.find ({name: " little whirlpool " })
 # [official] recommended the return value json data in new ways plus 3.2 
db.tablename.findOne ({name: " Henry " }) # query the first data qualifying

change

# Content modifier need $ SET 
# according to the query conditions, to modify the first data does not meet the conditions recommended] [official 
db.tablename.update ({query}, {$ modifier: {}} modifying properties) 
db.stutent.update ({name: " henry " }, {$ the SET: {age: 16}}) # no age has been created on the amendment 
# [recommended] official 
db.stutent.updateOne ({name: " henry " }, {$ SET: {Sex: " MALE " }}) # modify a data 
db.stutent.updateMany ({name: " Henry " }, {$ SET: {Sex: " FEMALE " , Hobby: " drink " } }) # modify all data in qualifying

delete

# Delete all eligible official data [not recommended] 
db.tablename.remove ({query})
 # delete the table 
db.tablename.drop ()
 # delete library 
db.dropDatabase ()
 # [official] recommended 
db.tablename .deleteOne () # delete the first qualifying data 
db.tablename.deleteMany () # delete all eligible data

 

Guess you like

Origin www.cnblogs.com/songzhixue/p/11202897.html