"Interview and abuse of vegetables"--MONGODB knowledge finishing

Source: Internet
Author: User
Tags failover

Why should we use MongoDB?

Characteristics:

    • High performance, easy to deploy, easy to use, easy to store data. The main features are:
    • For collection storage, easy to store data for object types.
    • Mode of freedom.
    • Supports dynamic queries.
    • Supports full indexes, including internal objects.
    • Support Queries.
    • Supports replication and recovery.
    • Use efficient binary data storage, including large objects such as video.
    • Automatically process fragmentation to support scalability at the cloud level
    • Drivers for Python,php,ruby,java,c,c#,javascript,perl and C + + languages are supported, and drivers for Erlang and. NET platforms are also available in the community.
    • The file storage format is Bson (an extension of JSON).
    • can be accessed over the network.

Function:

    • Collection-oriented storage: suitable for storing objects and data in JSON form.
    • Dynamic Query: The MONGO supports rich query expressions. Query directives use a JSON-style tag to easily query objects and arrays embedded in the document.
    • Full index support: includes embedded objects and arrays in the document. The query optimizer of MONGO parses the query expression and generates an efficient query plan.
    • Query monitoring: MONGO contains a monitoring tool for analyzing the performance of database operations.
    • replication and automatic failover: The MONGO database supports data replication between servers and supports replication between master-slave mode and server. The primary goal of replication is to provide redundancy and automatic failover.
    • Efficient Traditional storage: supports binary data and large objects (such as photos or pictures)
    • Auto-sharding to support cloud-scale scalability: automatic sharding supports a level of database clustering, adding additional machines dynamically.

Applicable occasions:

    • Website data: MONGO is ideal for real-time inserts, updates and queries, as well as the replication and high scalability required for real-time data storage on the site.
    • Caching: Because of its high performance, MONGO is also suitable as a caching layer for the information infrastructure. After the system restarts, the persistent cache layer built by MONGO can avoid overloading the underlying data sources.
    • Large, low-value data: Storing some data in a traditional relational database can be expensive, and many times programmers often choose traditional files for storage.
    • Highly scalable scenario: The MONGO is ideal for databases made up of dozens of or hundreds of servers. Built-in support for the MapReduce engine is already included in the roadmap for MONGO.
    • Storage for objects and JSON data: The MONGO Bson data format is ideal for storing and querying in a document format.

MongoDB to pay attention to the problem

1 because MongoDB is fully indexed, it directly puts the index in memory, so it supports up to 2.5G of data. If the 64-bit will be more.

2 because there is no recovery mechanism, so do a good job of data backup

3 because the default listener address is 127.0.0.1, it is not safe to authenticate, or it is recommended to configure it as localhost hostname

4 ensure change through GetLastError. ( this does not understand, actually did not use )

MongoDB Structure Introduction

The objects stored in MongoDB are Bson, a JSON-like binary file that consists of a number of key-value pairs. As shown below

{  "name": "Huangz",  "age": +, "  sex": "Male"  }  {    "name": "Jack",  "Class": 3,   "Grade ": 3  

The overall structure of the database is composed of the following:

Key-value pairs--"document--" collection--"database

MongoDB file size not more than 4M, but the new version can be increased to 16M

The key naming rules in MongoDB are as follows:

    • "\" cannot be used
    • With "." Number, "_" and "$" prefix are reserved
    • There are differences in case, age is different from age
    • The same document cannot have the same key
    • All other UTF-8 characters can be used except for the above rules
Common commands

1 #进入数据库

Use admin

2 #增加或修改密码

Db.adduser (' Xingoo ', ' 123 ')

db.adduser ("Xingoo", "123", true) parameters are user name, password, read-only

3 #查看用户列表

Db.system.users.find ()

4 #用户认证

Db.auth (' Xingoo ', ' 123 ')

5 #删除用户

Db.removeuser (' Xingoo ')

6 #查看所有用户

Show Users

7 #查看所有数据库

Show DBS

8 #查看所有的collection集合

Show collections

9 #查看各个collection的状态

Db.printcollectionstats ()

Ten #查看主从复制状态

Db.printreplicationinfo ()

#修复数据库

Db.repairdatabase ()

#设置profiling, 0:off 1:slow 2 All

Db.setprofilinglevel (1)

#查看profiling

Show profiling

#拷贝数据库

Db.copydatabase (' xingootest ', ' xingootest1 ')

Db.copydatabase ("Xingootest", "temp", "127.0.0.1")

#删除集合collection

Db.xingootest.drop ()

#删除当前数据库

Db.dropdatabase ()

MongoDB additions and Deletions change command

1 #存储嵌套的对象

Db.foo.save ({' name ': Xingoo, ' age ': +, ' address ': {' city ': ' Changchun ', ' Province ': ' Jilin '}})

2 #存储数组对象

Db.foo.save ({' name ': Xingoo, ' age ': +, ' address ': [' Jilin Province ', ' Liaoning Province ']})

3 #根据query条件修改, insert if not present, allow multiple records to be modified

Db.foo.update ({' Age ': '},{' $set ': {' name ': ' Xingoo '}},upsert=true,multi=true)

4 #删除yy Records of =5

Db.foo.remove ({' name ': ' Xingoo '})

5 #删除所有的记录

Db.foo.remove ()

Index

1 #增加索引: 1 asc-1 desc

Db.foo.ensureIndex ({firstname:1,lastname:-1},{unieap:true})

2 #索引子对象 (not understood)

Db.foo.ensureIndex ({' Al.em ':!})

3 #查看索引信息

Db.foo.getIndexes ()

Db.foo.getIndexKeys ()

4 #根据索引名删除索引 (not understood)

Db.foo.dropIndex (' al.em_1 ')

Inquire

Conditional operator

1 $GT---->2$LT----<3 $gte---- >=4$lte----<=5 $ne---- !=, <>6 $in---- in7 $nin---- not in8 $all---- All9 $or----orTen $not----Anti-match

1 #查询所有记录

Db.foo.find ()----SELECT * from foo

2 #查询某列非重复的记录

Db.foo.distinct ("Xingoo")----SELECT distinct name from foo

3 #查询age = 22 of records

Db.foo.find ({"Age": $})----SELECT * from foo where age = 22

Records for 4 #查询age > 22

Db.foo.find ({age:{$gt:)----SELECT * from foo where age > 22

Records for 5 #查询age < 22

Db.foo.find ({age:{$lt:)----SELECT * from Foo where age < 22

6 #查询age Records of <= 25

Db.foo.find ({age:{$lte: 25}})

7 #查询age >= 23 and age <=26 records

Db.foo.find ({age:{$gte:%, $lte: 26}})

8 #查询name中包含xingoo的数据

Db.foo.find ({name:/xingoo/})----SELECT * from foo where name like '%xingoo% '

9 #查询name中以xingoo开头的数据

Db.foo.find ({name:/^xingoo/})----SELECT * from foo where name like ' xingoo% '

Data for #查询指定列name, age

Db.foo.find ({},{name:1,age:1})----Select Name,age from foo

One #查询制定列name, age data, and age > 22

Db.foo.find ({age:{$gt: 22}},{name:1,age:1})----Select Name,age from foo where age >22

#按照年龄排序

Ascending:db.foo.find (). Sort ({age:1}) descending:db.foo.find (). Sort ({age:-1})

Data #查询name =xingoo.age=25

Db.foo.find ({name: ' Xingoo ', age:22})----SELECT * from foo where name= ' Xingoo ' and age = ' 25 '

14# Query the first 5 data

Db.foo.find (). Limit (5)----SELECT Top 5 * from Foo

#查询10条以后的数据

Db.foo.find (). Skip (Ten)----select * from foo where ID not in (select top of * from Foo);

Data between #查询在5-10

Db.foo.find (). Limit (Ten). Skip (5)

#or与查询

Db.foo.find ({$or: [{age:22},{age:25}]})----SELECT * from foo where age=22 or an age =25

#查询第一条数据

Db.foo.findOne (), Db.foo.find (). Limit (1)----SELECT top 1 * from Foo

#查询某个结果集的记录条数

Db.foo.find ({age:{$gte:). COUNT ()----SELECT COUNT (*) from Foo where age >= 20

#按照某列进行排序 (not understand)

Db.foo.find ({sex:{$exists: true}}). COUNT ()----select count (Sex) from Foo

#查询age取模10等于0的数据

Db.foo.find (' this.age% 10 = = 0 '), Db.foo.find ({age:{$mod: [10,0]}})

#匹配所有

Db.foo.find ({age:{$all: [22,25]}})

Record #查询不匹配name =x* lead

Db.foo.find ({name:{$not:/^x.*/}})

#排除返回age字段

Db.foo.find ({name: ' Xingoo '},{age:0})

#判断字段是否存在

Db.foo.find ({name:{$exists: true}})

Management

1 #查看collection数据大小

Db.xingootest.dataSize ()

2 #查看collection状态

Db.xingootest.stats ()

3 #查询所有索引的大小

Db.xingootest.totalIndexSize ()

Resources:

"MongoDB Introduction and Installation" Http://database.51cto.com/art/201103/247882.htm

"Getting Started with MongoDB" http://www.linuxidc.com/Linux/2013-01/78251.htm

"Interview and abuse of vegetables"--MONGODB knowledge finishing

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.