MongoDB structure, terminal, CRUD

  1. Mongodb structure

    1. Databases Server: After installing mongodb, when the mongodb program is started, the databases server is started.

    2. Databases: There are multiple databases in the databases server.

    3. Collections: There are multiple collections in each database. For example, in a school system, teachers and students are separate collections.

    4. Documents: There are multiple documents in each collection, similar to json (ie object) format. For example {_id: ObjectId('xxx1'), name: 'Tom', grade: 9} is a document.

    5. Fields: In the json-like format file of Document, each key is a field.

      MongoDB structure


    There are multiple clients that can access the database

    1. mongo shell: command line program

    2. mongodb compass: graphical interface, all operations are realized by clicking, similar to the github desktop version.

    3. script: code, such as JS


  2. type of data

    Data types are similar to JS. Common data such as:

    _id: int32 (The default is ObjectId, which contains some random numbers and machine physical information to ensure non-repetition)

    Name: Text (string)

    Hobbies: array (array of string)

    Address: object (embeded document)

    Wishes: array (array of documents)

    Boolean

    Number (can be subdivided into int32 (default), int64, float)

    Date (ISODate (default), timestamp)


  1. Mongodb command line basic operations, add, delete, modify and check

    1. show dbs: Display database information, databases in database server. Default admin, config and local.


    2. show collections: Display all collection names in the current database.


    3. use databaseName: create a new database (when the database name does not exist), or switch to a database (when the database name exists).


    4. Add a single new collection and documents (additional):

      1
      db.collection.insertOne()

      The db object refers to the currently used database, such as the school database. The collection name is to create a new collection collection (if the collection name does not exist), or use an existing collection collection in the database (if the collection name exists). insertOne() is to add document, the parameter is document (object, ie json). like:

      1
      db.students.insertOne({"name":"mason"})

      Return log information:

      1
      2
      3
      4
      {
      "acknowledged" : true, #Indicates that the server has received the request
      "insertedId" : ObjectId("6291fd3083ebd0086fc7152a") #Mongodb automatically creates an ID
      }

      You can also add multiple documents at a time, that is, pass an array with multiple documents in the array:

      1
      db.students.insertOne([{"name":"Raymond"},{"name":"Siri"}])

      Multiple _ids will be generated corresponding to multiple documents.


    5. Find the data in the current collection (find multiple) (check), and return the result

      1
      db.collection.find() #such as db.students.find()
      1
      { "_id" : ObjectId("6291fe3f83ebd0086fc7152b"), "name" : "Derek" } #Return result

      Find data in the current collection (find one, and be the first data in the current collection)

      1
      db.collection.findOne()

      Find the specified condition data, the condition is that the value of a field is xxx. Note that the key here does not have " " .

      1
      db.collection.findOne({name:"max"})

      find() actually has a second parameter, which is the display condition (which fileds are displayed in the returned result). The field value to be displayed in the display condition is 1, and the default is 0. But _id defaults to impressions, i.e. the value is 1.

      1
      db.students.find({}, {name:1, _id:0})

    6. Update data (change single) (change). Receive two parameters, parameter 1 is how to filter the data (find the first matching document), parameter 2 is what to update the found document. The operator operator is used when updating, which is called update operator: $set:{} .

      1
      db.collection.updateOne({name:"Derek"},{$set:{"name":"James"}})

      It is also possible to add new fields:

      1
      db.collection.updateOne({name:"James"},{$set:{"Hobbies":["football","gaming"]}})

      Update data (change multiple) to match all eligible documents.

      1
      db.collection.updateMany({name:"James"},{$set:{"Hobbies":["football","gaming"]}})

      There is also a db.collection.replaceOne() method for complete replacement.


    7. Delete a document. Filter first, then delete the target document. The syntax is the same as search, means the key does not have " " .

      1
      db.collection.deleteOne()
      1
      db.students.deleteOne({name:"Derek"})

      Delete multiple documents. Note that deleteMany({}) operation is very dangerous, which means that all documents can be matched and all documents in the current collection will be deleted. This command is not used during development.

      1
      db.collection.deleteMany()

      Note that if you delete all documents in the current collection, the collection still exists; to delete the current collection, the command is db.collectionname.drop(). Information including indexes will also be deleted.


  2. Delete a document. Filter first, then delete the target document. The syntax is the same as search, means the key does not have " " .

    1
    db.collection.deleteOne()
    1
    db.students.deleteOne({name:"Derek"})

    Delete multiple documents. Note that deleteMany({}) operation is very dangerous, which means that all documents can be matched and all documents in the current collection will be deleted. This command is not used during development.

    1
    db.collection.deleteMany()

    Note that if you delete all documents in the current collection, the collection still exists; to delete the current collection, the command is db.collection.drop(). Information including indexes will also be deleted.


  3. MongoDB command line related tips

    1. The tab key can be filled with the collection name, that is, just write a collection name at the beginning. The logic is the same as the Node.js path.

    2. If the data brackets in the inserted document are missing (a bracket is not closed), the returned result will be ... . The solution is to type both open and closed when parentheses are entered, and then return the input content.

    3. You can also pass in an empty document: {} . Still unique because of the _id.

    4. You can also specify _id when adding a document, such as:

      1
      db.students.insertOne({"_id":"6291fe3f83ebd0086fc7152b","name":"mason"})

      This works fine. But compared to the automatically generated id, the specified _id is not wrapped in ObjectId(), means the type is different. The _id specified by yourself is of type string, and the automatically generated _id is of type ObjectId. There will be a third-party package to help us type conversion during development, so don’t care.


  1. operator operator

    operator will start with $. Official Detailed Documentation

    1. update operator Update operator. $set
    2. query operator query operator. $exists, the syntax is { field: { $exists: } } . When boolean is true, $exists matches documents that contain fields, including documents whose field value is null; when boolean is false, $exists returns documents that do not contain the corresponding field.
    3. projection operator display operator (for fetching data)

Share