MongoDB study notes (b) CRUD

1, the basic concept

Because MongoDB and we often use relational database have some connection here to give you that look, easy analogy learning

Relational Database MongoDB
database database
table set
Row File
Row Field

(1) Database

MongoDB single instance can accommodate a plurality of separate databases, different databases placed in different files

(2) collection

MongoDB collection is a collection of documents, similar to the concept of tables in a relational database

Set no fixed structure, and can insert different types of data formats in the set, but the data normally has a certain relevance

(3) Documents

Document is a set of key-value pairs, similar to the concept of relational database rows, and its data structure is basically the same JSON

A document need not set the same field, and the same type of data field need not be identical, but the key-value pair in the document must be ordered

2, database operations

MongoDB in the default database to test, if you do not create a new database, the collection will be stored in the test database default

(1) create a database

use DATABASE_NAME

If the database exists, then switch to the specified database; if the database does not exist, create database

For example, the following command will create myDB database:

> use myDB
switched to db myDB

(2) reviewing existing database

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

Note : The database you just created does not appear in the list of databases, inserting data into the database only after the newly created, will be displayed

(3) view the current database

> db
myDB

(4) delete the current database

> db.dropDatabase()
{ "ok" : 1 }

3, the operation set

(1) create a collection

db.createCollection(
    COLLECTION_NAME,
    {
        capped: <boolean>,      // 可选,若为 true,则创建固定集合,且此时必须要指定 size 参数
        autoIndexId: <boolean>, // 可选,若为 true,则自动在 `_id` 字段创建索引
        size: <number>,         // 可选,为固定集合指定可以使用的最大空间(以字节计)
        max: <number>           // 可选,为固定集合指定包含文档的最大数量
    }
)

Note : in MongoDB, insert the document again after you create a collection, the collection will really create

For example, the following command will create myCol collection myDB database:

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

(2) reviewing existing collection

> show collections
myCol

(3) Delete Collection

db.COLLECTION_NAME.drop()

For example, the following command will delete myCol collection myDB database:

> db.myCol.drop()
true

4. Document Actions

(1) into a document

db.COLLECTION_NAME.insert(document)

Note : If the collection is not in the database, MongoDB will be automatically created and inserted into the document collection

For example, the following command will the collection to myDB database myCol into the document:

> use myDB
switched to db myDB
> db.myCol.insert({"name":"MongoDB"})
WriteResult({ "nInserted" : 1 })

(2) query document

db.COLLECTION_NAME.find(
    query,      // 可选,指定查询文档的条件
    projection  // 可选,使用投影操作指定返回的键,默认省略,表示返回文档中所有的键
)

For example, the following command will query the document name field is equal to the MongoDB:

> db.myCol.find({"name":"MongoDB"})
{ "_id" : ObjectId("5c7c069826cb01475e68f64e"), "name" : "MongoDB" }

(3) update document

db.COLLECTION_NAME.update(
    query,      // 指定被更新的对象
    update,     // 指定更新对象
    {
        upsert: <boolean>,  // 可选,指定如果不存在被更新的对象时是否插入更新对象,默认为 false
        multi: <boolean>,   // 可选,若为 true,则更新所有符合条件的记录,默认为 false
        writeConcern: <document>    // 可选,指定抛出异常的级别
    }
)

For example, the following command set will be updated myCol myDB database in a data field name:

> db.myCol.update({"name":"MongoDB"},{$set:{"name":"MySQL"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

(4) delete the document

db.COLLECTION_NAME.remove(
    query,  // 指定被删除的对象
    {
        justOne: <boolean>,         // 可选,若为 true,则只删除一个文档,默认为 false
        writeConcern: <document>    // 可选,定义抛出异常的级别
    }
)

For example, the following command will delete the name field myCol collection myDB database is equal to the MySQL documentation:

> db.myCol.remove({"name":"MySQL"})
WriteResult({ "nRemoved" : 1 })

5, the query

description Operators format
equal {<key>:<value>}
not equal to $ does {<key>:{$ne:<value>}}
Less than $lt {<key>:{$lt:<value>}}
Less than or equal $ lte {<key>:{$lte:<value>}}
more than the $gt {<key>:{$gt:<value>}}
greater or equal to $gte {<key>:{$gte:<value>}}
AND Conditions {<key1>:<value1>,<key2>:<value2>}
OR Conditions $or {$or:[{<key1>:<value1>},{<key2>:<value2>}]}
Regular Expressions {<key>:/<REGULAR_EXPRESSION>/}

6, a processing method

  • pretty() The method may be formatted output:
db.COLLECTION_NAME.find().pretty()
  • limit() The method of reading a certain number of specified number of records, the number of parameters specified for NUMBER
db.COLLECTION_NAME.find().limit(NUMBER)
  • skip() The method specified number of records to skip a certain number, the number of parameters specified for NUMBER
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
  • sort() A method for sorting data, sorted according to the parameter which specifies KEY key

The VALUE parameter specifies what sort mode, if it is a specified ascending order, if the descending order is designated -1

db.COLLECTION_NAME.find().sort({KEY:VALUE})

[Read more MongoDB series of articles, look at MongoDB study notes ]

Guess you like

Origin www.cnblogs.com/wsmrzx/p/11564282.html