MongoDB knowledge Summary

A term

RDBMS MongoDB
database database
form set
Row File
Row Field
Table union Nested documents
Primary key The primary key (key MongoDB to provide the _id)

database

Database name may be any of the following conditions are UTF-8 string.

  • It can not be an empty string ( "").
  • Shall not contain '' (space),., $, /, \ And \ 0 (null character).
  • It should be all lowercase.
  • Up to 64 bytes.

Some database names are reserved and can directly access these databases have a special role

  • admin
  • local
  • config

set

MongoDB is a collection of documents group, similar to the RDBMS: table (relational database management systems Relational Database Management System).
Collection exists in the database, a collection of no fixed structure, which means that you can insert in the collection of data in different formats and types, but usually we insert a set of data will have some relevance.
When the first document is inserted, the set will be created.
Legal collection name

  • Collection name can not be empty string "."
  • Collection name can not contain the \ 0 character (null character), this character represents the end of the collection name.
  • Collection name can not be "system." At the beginning, which is the prefix for the collection system reserved.
  • User-created collection name can not contain reserved characters. Some drivers do support included in the set were there, this is because the collection contains some system-generated character. Unless you want to access this collection created by the system, or do not appear in $ name inside.

File

Document is a set of keys (key-value) for (ie BSON). MongoDB's document does not need to set the same field, the same field and does not require the same data type, which is very different from relational databases, MongoDB is a very prominent feature.
have to be aware of is:

  • Document key / value pairs are ordered.
  • Value document may be not only the inside of the string in double quotes, may also be several other types of data (or even the entire embedded document).
  • MongoDB differentiate the type and sensitive.
  • MongoDB document can not have duplicate keys.
  • Key document is a string. With few exceptions, the key may be any UTF-8 characters.

Document key naming conventions:

  • Key can not contain \ 0 (null character). This character is used to indicate the end of the bond.
  • . $ Have special meaning and can only be used under certain circumstances.
  • Underscore "_" at the beginning of keys are reserved (not strictly required).

Field

Second, the commonly used commands

1. Connect database

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

Examples of
connection to the local database server, port is the default. .

mongodb://localhost

User name fred, login password foobar localhost the admin database.

mongodb://fred:foobar@localhost

User name fred, login password foobar localhost database of baz

mongodb://fred:foobar@localhost/baz

other...

2. Check all databases

show dbs

3. Review the current database

db

4. Create a database connection to the specified database use database if the database does not exist.

use xxx

5. Delete Database

First with the specified database to use xxx xxx, and then delete command db.dropDatabase ().

db.dropDatabase()

6. Review the existing collection

show collections

7. Create a collection

db.createCollection(name, options)

Examples

db.createCollection("col")

In MongoDB, you do not need to create a collection. When you insert some documents, MongoDB will automatically create a collection

db.mycol2.insert({"name" : "菜鸟教程"})

Note:! In MongoDB collection is created only when content is inserted that is, create a collection (data sheet) again after the insertion of a document (record), the collection will really create.

8. Delete Collection

db.collection.drop()

Examples

db.mycol2.drop()

9. View the document

find () to view the document, in addition to find () method, there are a findOne () method simply returns a document.

db.COLLECTION_NAME.find()

Conditional operator

operating format example
equal {<key>:<value>} db.col.find ({ "by": "novice Tutorial"}) pretty ().
Less than {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}).pretty()
less than or equal to {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}).pretty()
more than the {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}).pretty()
greater than or equal to {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}).pretty()
not equal to {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}}).pretty()

AND condition and OR condition

The MongoDB find () method can be passed a plurality of keys (key), each key (key) separated by a comma, i.e. a conventional SQL AND condition.

db.col.find({"by":"菜鸟教程", "title":"MongoDB 教程"})

MongoDB OR conditional statement uses the keyword $ or, syntax is as follows

db.col.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
)

Examples

db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鸟教程"},{"title": "MongoDB 教程"}]}).pretty()

$type

$ Type operator is set to retrieve the data type matching, and returns a result based on the type of BSON.

db.col.find({"title" : {\$type : 'string'}})

Limit and Skip methods

limit () method of the basic syntax is as follows:

db.COLLECTION_NAME.find().limit(NUMBER)

In addition to using limit () method reads the specified amount of data, you can also use the Skip () method to skip a specified number of data, Skip method also accepts as a parameter a number of the number of records to skip

db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)

10. Insert documents

insert () or save () method to insert a document to the collection, the following syntax:

db.COLLECTION_NAME.insert(document)

Examples

db.col.insert({mycol: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: '菜鸟教程',
    url: 'http://www.runoob.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

Insert the document can also be used db.col.save (document) command. If no _id field save () method is similar to insert () method. If you specify _id field _id of the data is updated.

11. Update Document

update () and save () method to update the collection of documents.
update () method is used to update the existing document. Syntax is as follows:

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

Parameter Description:

  • query: query update, similar to the back where within sql update query.
  • update: update objects and some of the newer operators (e.g., $, $ inc is ...) and the like, may be understood as a set behind the sql update query
  • upsert: Optional, this parameter mean, if there is no record update, whether to insert objNew, true inserted, the default is false, not inserted.
  • multi: Optional, mongodb default is false, only updating the first record found, if this parameter is true, according to the conditions put many records to check out all the updates.
  • writeConcern: optional level of an exception is thrown

Examples of
updating the title (title) by update () method

db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})

save () method to replace the existing document by the incoming document. Syntax is as follows

db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)

Examples of
the following examples we replace the _id of the document data 56064f89ade2f21f36b03136:

db.col.save({
    "_id" : ObjectId("56064f89ade2f21f36b03136"),
    "title" : "MongoDB",
    "description" : "MongoDB 是一个 Nosql 数据库",
    "by" : "Runoob",
    "url" : "http://www.runoob.com",
    "tags" : [
            "mongodb",
            "NoSQL"
    ],
    "likes" : 110
})

12. Delete the document

remove () function is used to remove the data set. Syntax is as follows:

db.collection.remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)

Parameter Description:

  • query :( Optional) condition for the document to delete.
  • justOne: (optional) If true or 1, only delete a document, if you do not set this parameter, or use the default value is false, all documents matching conditions are deleted.
  • writeConcern :( optional) throw-level exception.
    Examples
db.col.remove({'title':'MongoDB 教程'})

Guess you like

Origin www.cnblogs.com/superlizhao/p/12030729.html