MongoDB (C): a database operation, collective operation

1. database operations

After reading the previous article, we should build a good environment, let's start learning some basic operations of the MongoDB.

First we have to understand some of the points:

MongoDB stores data as a document, a data structure of the key (key => value) Composition

MongoDB document similar to JSON objects, field value can contain other documents, arrays, array of documents

The second is the comparison of the terms and MongoDB SQL terms:

MySQL students learned very good contrast, the three major: database, collection, documentation

A collection is in MySQL table

MySQL documentation is in line

Documentation: is an object made up of the key, is an extension of json Bson form.

{ ' Name ' : ' Guojing ' , ' gender ' : ' man ' }

Set: MySQL similar tables, storing a plurality of documents, the structure is not fixed, as can be stored as a document in the collection.

{ ' Name ' : ' Guojing ' , ' gender ' : ' man ' }
{'name':'huangrong','age':'18'}
{'book':'shuihuzhuan','heros':'108'}

Database: is a set of physical containers, a database may contain a plurality of documents.

A server usually has multiple databases.

1.1 Creating a database

MongoDB uses use DATABASE_NAME command to create the database. If the specified database DATABASE_NAME does not exist, the command will create a new database, otherwise the existing database.

grammar:

use DATABASE_NAME

If you want to create a name for newdb database, then use the use DATABASE_NAME statement is as follows:

> use newdb
switched to db newdb

1.2 Check the currently selected database

Enter the db command can query:

>db
newdb

1.3 View database list

If you want to check the list of databases, use the command show dbs.

>show dbs
local     0.000025GB
test      0.00002GB

Create a database newdb not in the list. To display the database, you need to insert at least one document, empty database does not show up.

>db.items.insert({"name":"yiibai tutorials"})
>show dbs
local     0.00005GB
test      0.00002GB
newdb      0.00002GB

The default database in MongoDB is: test. If you have not created any database, then the set / documents will be stored in testthe database.

1.4 Delete Database

db.dropDatabase MongoDB in () command is used to delete an existing database. 

db.dropDatabase()

This will remove the currently selected database. If you do not select any database, it will delete the default test database.

Delete example:

First, use the command show dbs a checklist of available databases. 

>show dbs
local      0.00025GB
newdb       0.0002GB
test       0.00012GB
>

If you want to delete the new database newdb, then the dropDatabase()command would look like this:

>use newdb
switched to db newdb
>db.dropDatabase()
>{ "dropped" : "newdb", "ok" : 1 }
>

Now check the list of databases

>show dbs
local      0.00025GB
test       0.0002GB
>

2. set operations

Create Collection 2.1

MongoDB's db.createCollection (name, options) method used to create a collection in MongoDB. 

grammar:

db.createCollection(name, options)

In command, name is the name of the collection to be created. options is a document that is used to configure the specified collection.

options parameter is optional, it is only necessary to specify the name of the collection. The following is a list of options you can use:

When a document is inserted, MongoDB first check limit set cappedthe field size, then check maxfield.

example: 

The basic syntax does not use options createCollection () method is as follows:

>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>

可以使用命令show collections检查创建的集合。

>show collections
mycollection

以下示例显示了createCollection()方法的语法,其中几个重要选项

> db.createCollection("mycol", {capped : true, autoIndexId : true, size : 6142800, max : 10000 })
{ "ok" : 1 }
>

在MongoDB中,不需要创建集合。当插入一些文档时,MongoDB 会自动创建集合。

>db.newcollection.insert({"name" : "yiibaitutorials"})
>show collections
mycol
newcollection
mycollection
>

2.2 删除集合

MongoDB 的db.collection.drop()用于从数据库中删除集合。

语法:

db.COLLECTION_NAME.drop()

例子:

首先,检查数据库test中可用的集合。

>use test
switched to db test
> show collections
mycol
mycollection
newcollection
>

现在删除名称为mycollection的集合。

>db.mycollection.drop()
true
>

再次检查当前数据库的集合列表,如下:

> show collections
mycol
newcollection
>

Guess you like

Origin www.cnblogs.com/liuhui0308/p/11922692.html