Last active
May 24, 2020 09:42
-
-
Save anhquoc1104/838c5c058264a9e197783ab05f191428 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Colection == table; document == row/tuble; field == column | |
-> Connect DB | |
### - Show DB : show dbs - dont show empty DB. | |
### - use or create DB : use <DB-name> | |
### - Check DB using : db | |
### - insert collection : db.<coll-name>.insert({"name" :"xyz"}) | |
db.createCollection("name-Coll", {option}) - option : capped, autoIndexId, size, max. | |
### - show Collection : show collections | |
### - Drop DB (default "test" DB) : db.dropDatabase() | |
### - Drop Collection : db.<coll-name>.drop() | |
### - insert document : db.<coll-name>.insert(obj) - obj: a obj or arr of obj. | |
### - query Document : db.<coll-name>.find(obj-condition^) - return all result | |
db.<coll-name>.find(obj-condition^).pretty() - return format result | |
db.<coll-name>.findOne(obj-condition^) - return one result | |
=> condition: AND: $and: [obj1, obj2] ; OR: $or: [obj1, obj2] ; NOR: $nor: [obj1, obj2] ; NOT: $not: [obj1] | |
AND + OR : db.mycol.find({"likes": {$gt:10}, $or: [{"by": "admin"},{"title": "MongoDB"}]}).pretty() | |
Equality : {key:value}; | |
Less Than : {key:{$lt:value}}; | |
Less Than Equals : {key:{$lte:value}} | |
Greater Than : {key:{$gt:value}}; | |
Greater Than Equals : {key:{$gte:value}}; | |
Not Equals : {key:{$ne:value}} | |
### - update Document : db.<coll-name>.update({obj-find}, {$set:{obj-set}}) - Add value to document | |
db.<coll-name>.save({"_id": Object(...), key:value, key:value} - Repalce value to document | |
### - Remove Document : db.<coll-name>.remove() - Remove all document of collection | |
db.<coll-name>.remove({obj-remove}) - remove all doccument by condition | |
db.<coll-name>.remove({obj-remove}, 1//true) - remove 1 document first by condition | |
### - Profection : db.<coll-name>.find({},{key: 0//1}) - Select field necessary (0: hiden, 1: show) | |
### - Limit record : db.<coll-name>.find({}, {key: 0//1}).limit(num) - limit num record to be displayed | |
### - skip record : db.<coll-name>.find({}, {key: 0//1}).skip(num) - skip num record to be displayed | |
### - sort record : db.<coll-name>.find({}, {key: 0//1}).sort(key: 1*//-1) - sort record with -1: descending | |
### - Index : db.<coll-name>.createIndex({key: 1, key: -1}) - create Index for field | |
db.<coll-name>.dropIndex({key: 1//-1}) - drop 1 index | |
db.<coll-name>.dropIndexs({key: 1, key: -1} - drop many Index. | |
db.<coll-name>.getIndexs() - get all Index. | |
### - Aggregation : db.<coll-name>aggregate([{pipeline : {_id : "$by_user", num_tutorial : {expressions : 1}}}]) | |
pipeline: $project − $match − $group − $sort − $skip − $limit - $unwind | |
expressions: $sum - $avg - $min - $max - $push - $addToSet - $first - $last | |
### - | |
### - | |
### - |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment