Mongo database learning


Chapter 2. Basic knowledge of mongodb

1 documentation

A document is equivalent to a row in a relational database.

The key value pairs in the document are ordered.

2 set

A set is equivalent to a table.

2.1 dynamic mode

Suppose a field named “type” in the collection is used to indicate whether the document is a fruit or a vegetableDocuments of different types are split into two collections

2.2 subsets (in doubt)

Use “.” to separate subsets of different namespace. In mongodb, using subsets to organize data is very efficient and worth recommending.

3 Database

Multiple documents constitute a collection, and multiple sets constitute a database.

The database name should be all lowercase.

The database eventually becomes a file in the file system, so there are many restrictions on database names.

Self contained reserved database:

  • admin

    Corresponding to the “root” database, after adding a user, all database permissions will be automatically obtained.

  • local

    Never replicable

  • config

    Store fragment information

4 start up

Default startupC:\data\dbIf the directory does not exist, an error will be reported.

5 MongoDB shell

Shell is a fully functional oneJavaScriptInterpreter, runnableJavaScriptProcedures.

Mongo can be usedJavaScriptStandard library, definition and invocationJavaScriptFunction. help at the database level, help at the collection level.

5.1 create

>{"name":"Rohan","date":new Date()})

5.2 read

  • find()

    The shell automatically displays up to 20 matching documents

  • findOne()

5.3 update

Update takes at least two parameters

6. Data type

Mongo contains six data types in JSON:null, Boolean, number, string, array, and object. Other types are added on the basis of

  • null
  • Boolean type
  • Numerical type

    Default 64 bit floating-point value, integer eg:{"x" : NumberInt("3")}

  • character string
  • date

    {"x" : new Date()}

  • regular expression

    {"x" : /foobar/i}

  • array

    Can contain elements of different data types

  • Embedded document
  • Object ID

    {"x" : ObjectId()}

  • binary data
  • code

    Can contain any JavaScript code

Chapter 3. CRUD

3.1 insert{"bar" : "baz"})

  • Batch insertbatchInsert

    The received parameter is a nested dictionary in an array. In this case, multiple documents will be generated[{"name" : "Rohan"},{"sex": "None"}])

    The maximum message size for batch insertion is 48MB. When the message size exceeds 48MB, most drivers will split the request into multiple 48MB requests.

    When a document fails to be inserted in batch, the previous one is OK and the subsequent one cannot be carried out. If you expect to ignore the error, you can use thecontinueOnErrorOptions.

  • check

    All documents must be less than 16MB,Object.bsonsize(doc)View document size

3.2 deletion ({optional ": key value pair})

Permanent deletion, can not be undone, can not be restored.

  • Empty set (table)

3.3 update ({locator ': "value"}, {new set})

This operation completely replaces the target document with the new document, and even includes_idOf course_idYou can’t repeat it or you’ll fail.Use it with care!!!

3.3.1 modifier

  1. $setModifier

    Specifies the value of a field in the locator document. If this field does not exist, it will be created; if it exists, it will be updated: ({"user": "Rohan"}, {"$set": {favorite language: "Python"})

    Notice here$setFollowed by a dictionary is a nested document.

    $setYou can change the type of the value from a string to a list.

    $setYou can modify nested documents, eg: ({"user":"Rohan"},{"$set":{" ":" Shandong "}})

    $unsetYou can delete this key completely ({"user": "Rohan"}, {"$unset": {favorite language: 1}})

  2. $incValue modifier

    If the same field does not exist, it will be created; if it exists, it will be added on the original basis,It can only be a number,eg:{"user":"Rohan"},{"$inc":{"Age":25}})

Recommended Today

On the theoretical basis of SRE

What is SRE? When I first got into contact with SRE, many people thought that it was a post with full stack capability in Google and could solve many problems independently. After in-depth exploration, it is found that SRE can solve many problems, but there are too many problems. It is difficult for a post […]