//View all DatabasesShow DBS//Amdin 0.000GB//Local 0.000GB//Working with DatabasesUse admin//switched to DB admin//Create a database: Insert a record into the databaseDb.user.insert ({' name ': ' Wangxi ') })//Writeresult ({"ninserted": 1})//Show Current DatabaseDB//Admin//querying documents in the current collectionDb.user.find ()//{"_id": ObjectId ("59f7f0088cc643905a25fa7f"), "name": "Wangxi"}//Query by criteria (and condition query)Db.user.find ({name: ' Wangxi ', Age: ' 25 ') })//display all documents in a formatted fashionDb.col.find (). Pretty ()//returns only one documentDb.col.findOne ()//skips a specified quantity, returns a specified number of documentsDb.user.find ({}). Limit (3). Skip (1)//or condition queryDb.user.find ({$or: [{name: ' Wangxi '}, {name: ' Hanjiaren '}]})//{"_id": ObjectId ("59f821f8f4bf9a3582c6ef5c"), "name": "Wangxi"}//{"_id": ObjectId ("59f8222df4bf9a3582c6ef5f"), "name": "Hanjiaren"}//conditional operatorDb.user.find ({age: {$gt: 20}})//Greater thanDb.user.find ({age: {$lt: 30}})//less thanDb.user.find ({age: {$gte: 30}})//greater than or equalDb.user.find ({age: {$lte: 30}})//less than or equalDb.user.find ({age: {$eq: 25}})//equalsDb.user.find ({age: {$ne: 25}})//Not equal to//$type operator--Gets the document of the specified type (can look up the table for different types of documents corresponding to the number)Db.col.find ({title: {$type: 2 }})//Delete Current Databasedb.dropdatabase ()//{"Dropped": "admin", "OK": 1}//display all collections in the current databaseShow Collections/Show Tables//User//Delete the specified collectionDb.user.drop ()//true//Update documentDb.user.update ({name: ' Wangxi '}, {$set: {name: ' Raychan '}}, {upsert:true, Multi:true })//Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})Db.user.find ()//{"_id": ObjectId ("59f81c6f8cc643905a25fa87"), "name": "Raychan"}db.user.update ({name:' Wangxi '}, {$set: {name: ' Raychan '}},true,true) Db.user.find ()//{"_id": ObjectId ("59f81c6f8cc643905a25fa87"), "name": "Raychan"}//{"_id": ObjectId ("59f81ef674a03e811969aa93"), "name": "Raychan"}//Delete a documentDb.user.remove ({name: ' Raychan '},true)//Delete Only one document//Writeresult ({"nremoved": 1})//Delete all DocumentsDb.user.remove ({})//{} cannot be lessWriteresult ({"Nremoved": 5 })//Remove is obsolete and the official recommendation is to use Deleteone and DeletemanyDb.user.deleteOne ({name: ' Wangxi ')})//{"acknowledged": true, "Deletedcount": 1}//SortDb.user.find ({}). sort ({age:1})//aggregate $sum, $avg, $max, $min (field references are added $)Db.user.aggregate ([{$group: {_id: ' $name ', Num_of_user: {$sum: 1}}}])//select Name, COUNT (*) from the user group by name//Pipeline Operation//$project: Modifies the structure of the input document. You can use it to rename, add, or delete fields, or to create calculations and nested documents. Db.user.aggregate ({$project: {_id:0, name:1, age:0 }})//0 means no output, 1 means output is similar to the select name from user in SQL//$match: For filtering operations//filter documents for age > 20Db.user.aggregate ({$match: {age: {$gt: 20 }}})//$skip: Effects are the same as find (). Skip (num)Db.user.aggregate ({$skip: 3})
MongoDB Basic Operations Learning notes