- 1. Common commands
- show DBS Show all databases
- use dbname into dbname database, if not present,
- db View database
- show collections displays the collection in the database--equivalent to the table in MySQL!
- Create & Add
- db.users.save ({" Name ":" LECAF "}), created a collection called Users,
- db.users.insert ({" Name ":" Ghost "," Age ": 10}) inserts a new data into the Users collection, If you do not have the Users collection, MongoDB automatically creates
- save () and insert () also have a slight difference: if the new data primary key already exists, insert () will not do the operation and prompt error, and save () The original content is changed
- 3. Delete
- DB. Dropdatabase() Deleting a database
- Db.users.remove () Delete all data under the Users collection
- Db.users.remove ({"Name": "LECAF"}) deletes data NAME=LECAF under the Users collection
- Db.users.drop () or Db.runcommand ({"Drop", "users"}) Delete collection users
- Db.runcommand ({"Dropdatabase": 1}) Delete current data
- 4. Find
- Db.users.find (). Pretty () formatting data
- Db.users.find () Find all data in the Users collection
- Db.users.findOne () to find the first number in the Users collection
- Db.book.find ({' BookName ': ' Shuihuzuan '}) equals query
- db.book.find ({' Bookprice ': {$lt: 10000}}) is less than the query
- (>) greater than-$gt
- (<) less than-$lt
- (>=) greater than or equal-$gte
- (<=) less than or equal-$lte
- Db.book.find ({$or: [{' BookName ': ' Shuihuzuan '},{' Bookprice ': {$lt: 10000}]} or query
- Db.book.find ({' BookName ': ' Shuihuzuan ', ' Bookprice ': {$lt: 10000}}) and query
- Db.book.find (). Sort ({' Bookprice ': 1}) sorting
- Db.book.find (). Skip (1). Limit (2) paging
- Db.book.find ({' count ': {$exists: false}}). Count () Determine if the Count field exists
- Db.foo.find ({name:{$in: [null], $exists: true}}) query for NULL records
- 5. Modifications
- Db.Col.Update({ ' title ' :" MongoDB Tutorial '
- The above statement only modifies the first found document, and if you want to modify multiple identical documents, you need to set the multi parameter to True.
>Db.Col.Update({' Title ':' MongoDB Tutorial '},{$set: {' title ':' MongoDB '}}, {multi:true} ' set: Update field, such as No field increase table structure
- Db.users.update ({"Name": "LECAF"}, {"Age": 10}) modifies the NAME=LECAF data to age=10, the first parameter is the lookup condition, the second parameter is the modification, except the primary key, the other content is replaced by the contents of the second parameter , the primary key cannot be modified,
- Db.book.update ({},{$inc: {' Bookprice ': 600}},{multi:true}) price increased by 600
MongoDB General Command