Python_MongoDB

MongoDB commands

  • Database commands

    • View all databases
      show dbs  或者 show databases
    • View the current database
      db    Note: The test database is used by default without switching the database
    • Switch database or create database
      use db_name
    • Delete the current database
      db.dropDatabase()
  • Aggregate command

    • View all collections
      show collections
    • Create collection
      db.createCollection (name, options) 
      
      instance: 
      db.createCollection ( " sub " , {capped: true, size: 10}) 

      If there is no such collection, the first time you add data, it will automatically create a collection

      Description:

      1. Parameter capped: The default value is false means that the upper limit is not set, and the value is true means that the upper limit is set
      2. Parameter size: When the capped value is true, this parameter needs to be specified, which means that the upper limit is small. When the file reaches the upper limit, it will Overwrite the previous data in bytes
      3. Check if the collection has an upper limit: db. Collection name.isCapped ()

    • Delete collection
      db. collection name.drop ()
  • Add, delete, and modify commands for documents

    • insert
      • insert()
        db. collection name. 
        insert (document) 
        
        instance: db.stu.insert ({_ id: " 20200418 " , name: " wutongluo " , gender: 1})
      • save()
        db. collection name. save (document)

        Note: If _id is not set, it will be assigned by default

    • Inquire
      • find()
        db. collection name. find () 
        
        instance: 
        db.stu.find ()
      • pretty()
        db.collection name.find (). pretty () The data structure queried is clear and clear
    • Update
      db. collection name.update (<query>, <update>, {multi: <boolean> }) 
      
      instance:
           1.db. class .update ({name: ' python ' }, {name: " java " }) 
          modify The value of name is
           changed , but the other fields are missing. 2.db. class .update ({name: " php " }, {$ set: {name: " go " }}) The 
          name value is modified, other fields are not Change
           3.db. class .update ({}, {$ set: {num: 205 }}, {multi: true})
          

      Description:
      1. Parameter query: query condition
      2. Parameter update: update operator
      3. Parameter multi: optional, the default is false, which means that only the first record found is updated, and a value of true means that the file with full conditions Update all

    • delete
      . db collection name .remove (<Query>, {justone: <Boolean> }) 
      
      Example: 
      . db class .remove ({NUM: 205 }, {:} justone to true)  
       #   delete the first to meet the conditions

      Note:
      1. The parameter query: optional, the condition of the deleted file
      2. The parameter justOne: optional, if set to true or 1, only one item will be deleted, the default value is false, which means multiple items will be deleted

    • If there are any mistakes, you are welcome to point them out. Discuss issues together.

Guess you like

Origin www.cnblogs.com/wutongluo/p/12727462.html