MongoDB basic operation instructions

Database operations

  • Boot mongod --dbpathpath data stored (data used to store a file folder)
  • Use the database mongo(reopen a terminal)
  • Import Data mongoimport
  • List all databases show dbs
  • Use / Create Database use xxx
  • View the current database db
  • Display the collection in the current database show collections

Data addition, deletion and modification

1. Insert data

  • Insert a piece of data (as the specific data is inserted, the database is successfully created)
    db.tmp.insert({"name": "xxx"})
  • It is impossible to manually insert each piece of data, so you can write the database in the external form and import it into the specific database the database
    mongoimport --db item--collection tmp--drop --file D:\Users\xxx\xxx\xxx.json
     --db item  to be imported into the data
     --collection tmp  the specific collection of the database to import the gold
     --drop  data Clear the
     --file xxx.json  data in the item Externally write the data file

2. Delete data

  • Delete database (delete the current database)
    db.dropDatabase()
  • Delete collection
    db.tmp.drop()
  • Delete the document (delete all data, but the collection itself is still null)
    db.tmp.remove()
  • Delete document (delete all data whose name is Json)
    db.tmp.remove({"name":"xxx"})
  • Delete the document (delete the first data whose name is Json)
    db.tmp.remove({"name":"xxx"},{justOne:true})

3. Modify the data

  • Change the age of the first data with id 9999 to 100
    db.tmp.update({"id":"9999"},{$set:{"age":10}})
  • Change the age of all sex data to 100
    db.tmp.update({"sex": "男"}, {$set: {"age": 10}}, {multi:true})
  • Complete replacement / rewriting (the $ set keyword does not appear)
    db.tmp.update({"id": "9999"}, {"name": "ccc", "age":10})
  • Method for adding data to data of one record in database
    db.tmp.update({"id": "9999"}, {$addToSet:{comment: {"name" : "xxx", "sex" : "xxx", "age" : "xxx"}}})

4. Query data

  • Find the database (already entered a specific database)
    use item into the database
    show collections list collection
  • Find all the data in the database
    db.tmp.find()
    db.getCollection('add').find()
  • Exact match 
    db.tmp.find({"name":"xxx"})
  • Multiple conditions 
    db.tmp.find({"name":"xxx","age":10})
  • Greater than condition 
    db.tmp.find({"age":{$gt:20}})
  • or   
    db.tmp.find({$or:[{"age":10},{"age": 100}]})
  • Sort   
    db.tmp.find().sort({"age": 1})
Published 40 original articles · won 31 · visited 2758

Guess you like

Origin blog.csdn.net/CodingmanNAN/article/details/105678630