Basic introduction to the MongoDB tutorial

  • 2020-05-12 06:24:58
  • OfStack

1. Notes for documents:

1. The key value pairs are ordered, e.g. {"name" : "stephen", "genda" : "male"} is not equal to {"genda" : "male", "name" : "stephen"}
2. Document information is case sensitive, e.g. {"name" : "stephen"} not equal to {"Name" : "stephen"}
3. Document information is type-specific, such as: {"age" : 30} not equal to {"age" : "30"}
4. Duplicate keys are not allowed in the document, such as: {"name" : "stephen", "name" : "liu"}

2. Necessity of using multiple collections:

1. It would be a disaster for a developer to have all the schema documents in one collection. Because once you get the query results, you have to manually write code to filter the different types of documents.
2. It will reduce the query efficiency. Imagine 1, assuming that a schema document has a relatively small amount of data, and if you still put it into a generic large set, the query efficiency is bound to be much lower than if you put it into a separate set.
3. Index utilization will be more efficient if all documents are in the same schema when creating indexes.

3. Matters needing attention in naming the collection:
1. The collection name cannot be an empty string "".
2. Do not start with system, which is generally reserved for the system, as the system.users collection holds the user information of the database, while the system.namespace collection holds the information of the database collection.
3. Do not include the '$' character in the collection name.
4. Subsets are only a good way to plan collections, such as blog.posts and blog.anthurs. In fact, they have no relationship with the blog set, and even the blog set does not exist.

4. Database:

Multiple databases can exist on the same MongoDB server, each with a different database stored in a different file. Because the database name is bound to the file name, the database name is limited.
1. Cannot be empty "".
2. All lowercase and no more than 64 bytes.
3. Do not include illegal characters in file name naming.
4. The admin database is the administrative database, and if a user is in the database, he will automatically inherit all database privileges. Certain server commands can only be run from this database.
5. local this database will never be replicated and will only be used to store any collection limited to a single local server.
The name of a collection that represents the fully qualified name of the collection, not longer than 121 by itself.

5: startup of MongoDB:

1. Directly execute mongod. Without any command-line arguments, the host of the server must contain the /data/db directory. For Windows, the default directory is \data\db of the drive where the service program is located. For D, D:\data\bin. The default listening port is 27017.
2. MongoDB comes with an JavaScript Shell that can interact with MongoDB from the command line. Such as: mongo. The Shell tool can perform simple mathematical operations directly. Such as:


    > x = 200
    200
    > x /5
    40
    -- You can also call JavaScript Standard library.
    > new Date("2012/05/05")
    ISODate("2012-05-04T16:00:00Z")
    > "Hello World".replace("World", "MongoDB")
    Hello MongoDB
    -- Define and invoke custom JavaScript Function.
    > function factorial(n) {
    ... if (n <= 1) return 1
    ... return n * factorial(n - 1)
    ... }
    > factorial(5)
    120

3. Insert the MongoDB document in the Shell client, as follows:


    > post = { "title" : "my blog post", "content" : "Here's my blog", "date" : new Date() }
    {
         "title" : "my blog post",
         "content" : "Here's my blog",
         "date" : ISODate("2012-06-04T07:38:51.345Z")
    }
    > db.blog.insert(post)
    > db.blog.find()
    { "_id" : ObjectId("4fcc661de4bcbac15b3d9e3a"), "title" : "my blog post", "content" : "Here's my blog",
    "date" : ISODate("2012-06-04T07:38:51.345Z") }

4. Query documents in Shell client, such as:


    > db.blog.findOne()
    {
         "_id" : ObjectId("4fcc661de4bcbac15b3d9e3a"),
         "title" : "my blog post",
         "content" : "Here's my blog",
         "date" : ISODate("2012-06-04T07:38:51.345Z")
    }

5. Update the document in Shell, as follows:


    -- Need to update first post The contents of the variable are first added 1 a comments Is an empty array.
    > post.comments = []
    [ ]
    --update The first 1 A parameter is a condition, number 2 Three parameters are the values to be updated.
    > db.blog.update({ "title" : "my blog post"}, post)
    > db.blog.findOne()
    {
         "_id" : ObjectId("4fcc661de4bcbac15b3d9e3a"),
         "title" : "my blog post",
         "content" : "Here's my blog",
         "date" : ISODate("2012-06-04T07:38:51.345Z"),
         "comments" : [ ]
    }

6. Delete in Shell client, as follows:

    -- if remove If there is no condition in, all data in the collection is cleared.
    > db.blog.remove( { title:"my blog post"})
    > db.blog.findOne()
    null

6. Tips for using Shell:

    > show dbs  -- Displays the database name.
    > show collections -- Display collection name
    > show users -- Display username
    > db.help()  -- Lists the methods for the database.
    > db.blog.help() -- list blog Methods on the set.
    > db.blog.update -- You can look at it directly update methods JavaScript Implementation code.


Related articles: