mongodb basic operations

close mongodb command

  1. use admin #Switch to admin user

    1. db.shutdownServer() #shutdown mongodb
  2. pkill mongod

show all databases

  1. show dbs

switch database

  1. use database name

delete database

  1. db.dropDatabase()

view all collections

  1. show collections

view all tables

  1. show tables

data writing

  1. db.collection_name.insert({x:1,y:2})
  2. db.collection name (such as mysql table name).insert (written json format data)

data query

  1. db.collection_name.find()
    1. query all data
  2. db.collection_name.find({x:1})
    1. Query data where x is equal to 1
  3. db.collection_name.find().count()
    1. Quantity that satisfies the condition
  4. db.collection_name.find().skip(3).limit(2).sort({x:1})
    1. skip: such as the offset of mysql
    2. limit: get the number of data
    3. sort: sort, 1 positive order, -1 reverse order

data update

  1. db.tab1.update({x:1},{x:999})
    1. Update the first piece of data with x equal to 1 to 999, only the first piece of data that satisfies the condition will be updated (only one piece of data will be updated)
  2. db.tab1.update({x:1},{$set:{x:999,y:888}})
    1. Existing fields will only update the value, non-existing fields will be incremented
  3. db.tab1.update({x:1},{x:999},true)
    1. Automatically write when updating non-existent data
  4. db.tab1.update({x:1},{$set:{x:999}},false,true)
    1. Update all data that meets the condition

deletion of data

  1. db.tab1.remove({x:1})
    1. delete data where x is equal to 1

mongodb create user

  1. Creation syntax: createUser (addUser before 2.6)
    1. db.createUser({user:”listen”,pwd:”listen”,roles:[{role:”userAdmin”,db:”demo”}]})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649083&siteId=291194637