Mongo database learning

Time:2020-10-18

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.

db.help()View help at the database level,db.foo.help()View help at the collection level.

5.1 create

> db.blog.insert({"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

db.foo.insert({"bar" : "baz"})

  • Batch insertbatchInsert

    The received parameter is a nested dictionary in an array. In this case, multiple documents will be generated

    db.foo.batchInsert([{"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

db.foo.remove ({optional ": key value pair})

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

  • Empty set (table)

    db.foo.drop()Faster

3.3 update

db.foo.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:

    do.foo.update ({"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:

    do.foo.update ({"user":"Rohan"},{"$set":{" address.company ":" Shandong "}})

    $unsetYou can delete this key completely

    do.foo.update ({"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:

    do.foo.update({"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 […]