Command operation of mongodb

Time:2021-10-19

Mongodb series:

Before introducing the specific operation and use of mongodb, it is necessary to introduce some basic concepts of mongodb. With these foundations and knowledge, you can master the following operations more solidly and understand them more thoroughly.

Mongodb database is different from relational database. It has three basic concepts: document, collection and library. Corresponding to the records, tables and libraries of the relational database respectively. The library must have a disk file to correspond to. The mongodb database stores documents in a collection. The documents in the collection do not need to have the same structure. Each document can have a different structure. This makes mongodb more flexible than traditional RDBMS because you don’t have to define the structure in advance.

Document is the core concept of mongodb, and document is the smallest unit to operate the database. First, let’s see what the document looks like. A document is actually an object literal in JavaScript, that is, a key value pair. It is precisely because the document of mongodb is the literal quantity of objects in JavaScript that mongodb becomes the preferred database system for node.js application development because it is very simple, intuitive and easy to use.

Here are some examples of documents to introduce the rules of documents:

  • The key / value pairs of documents are ordered, and the key / value pairs with the same content but different order are different documents;

    {
      "greeting": "hello world", 
      "foo": 3
    }
    {
      "foo": 3, 
      "greeting": "hello world"
    }
  • The value in the document can not only be a double quoted string, but also other data types, such as integer, null, Boolean, floating point number, binary data, array, code, etc., and even an embedded document;

    {
      "null": null,
      "boolean": true,
      "integer": 32,
      "string": "hello",
      "float": 3.14,
      "date": new Date(),
      "code": function() { console.log('hello'); },
      "array": [1, 3, 5, 7]
    }
  • The key of the document is a string. Except in rare cases, the key can make any UTF-8 character;
  • The key cannot contain 0 (null character), which is used to represent the end of the key;
  • . and $are of special significance and are generally not recommended;
  • The key at the beginning of the underscore is reserved and is generally not recommended;
  • The keys / values in the document are not only type sensitive, but also case sensitive;

    {
      "foo": 3
    }
    { 
      "foo": "3" 
    }
    
    { 
      "foo": 3
    }
    { 
      "Foo": 3 
    }
  • Duplicate keys cannot be used in the document;

    {
      "greeting": "hello world",
      "greeting": "hello MongoDB"
    }

The following describes the basic shell operations, including adding, deleting, modifying and querying documents in the mongodb database. When you understand these commands or functions, and then try to operate the database in JavaScript script code, you will find this API very easy to use.

$ mongo

>Use todo # switches the database. If the database does not exist, it will be created. If it exists, it will be switched
>DB # view the current database

>DB. Todo. Insert (data) # inserts data into the todo collection. Data is a legal document described above

#For example:
> db.todo.insert({"item": "play football"})

>DB. Todo. Find() # returns all documents in the collection
>DB. Todo. Findone() # returns a document in the collection

>DB. Todo. Update (old, new) # replace old documents with new ones

>Db.todo.remove (old) # delete old documents

For more command usage, refer to the documentation:Mongodb basic operation

Summary:

In fact, the API of shell is exactly the same as that of JavaScript programming. There are four words: insert, remove, update and find.

If this article is useful to you, please like it and collect it!