Skip to content

Instantly share code, notes, and snippets.

@nnhjs
Created May 1, 2020 09:23
Show Gist options
  • Save nnhjs/54a8c8b1b7dee62d05fc4c4c69cde98e to your computer and use it in GitHub Desktop.
Save nnhjs/54a8c8b1b7dee62d05fc4c4c69cde98e to your computer and use it in GitHub Desktop.
MongoDB
Create Database - use DATABASE_NAME
Show all databse - show dbs
show current database - db
drop databse - db.dropDatabase()
Create collection: db.createCollection(name, options)
Drop collection: db.COLLECTION_NAME.drop()
Data type: String, Boolean, Araay, Integer, Double, Object, Null, Symbol, Timecstamp, Date, Object ID, Binary Data, Code, Regular expression
* Insert Document: you can use method insert() or save()
db.COLLECTION_NAME.insert(document);
if conllection don't exit in database - severt create collection after insert document
db.COLLENTION_NAME.save(document) tương tự như insert nếu không có _id, nếu có _id thì sẽ thay thế toàn bộ dữ liệu _id như trong save()
db.COLLECTION_NAME.insertOne(document) - insert one document
db.COLLECTION_NAME.insertMany(documents) - insert many document
* Query Document
db.COLLECTION_NAME.find() - display all document in a non-structed way
db.COLLECTION_NAME.find().pretty() - dispaly result in a formatted way
db.COLLECTION_NAME.findOne( {key: value} ) - return only one document
AND in MongoDB
db.COLLECTION_NAME.find($and: [{ <key1>: <value1> }, { <key2>:<value2> } ]);
db.COLLECTION_NAME.find($or: [{ <key1>: <value1> }, { <key2>:<value2> } ]);
NOR in MongoDB
db.COLLECTION_NAME.find( { $nor [ { key1: value1 }, { key2: value2 } ] } )
NOT in MongoDb
db.COLLECTION_NAME.find( key: { $not: { value } } )
* Update Document
update() method
db.COLLECTION_NAME.update( SECLECTION_CRITERIA, UPDATED_DATA )
eg: db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
save() method
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
findOneAndUpdate() method
db.COLLECTION_NAME.findOneAndUpdate(SELECTION_CRITERIA, UPDATE DATA)
eg: > db.empDetails.findOneAndUpdate(
{First_Name: 'Radhika'},
{ $set: { Age: '30',e_mail: 'radhika_newemail@gmail.com'}}
)
updateOne() method - update single document
>db.COLLECTION_NAME.updateOne(<filter>, <update>)
eg: {First_Name: 'Radhika'},
{ $set: { Age: '30',e_mail: 'radhika_newemail@gmail.com'}}
updateMany() method
db.COLLECTON_NAME.updateMany(<filter>, <update>);
eg: db.empDetails.updateMany(
{Age:{ $gt: "25" }},
{ $set: { Age: '00'}}
)
* Delete Document
remove() method - nhận 2 tham số đầu vào remove(deletion criteria, justOne)
syntax: db.COLLECTION_NAME.remove(DELETION_CRITERIA);
Remove one - db.COLLECTION_NAME.remove(DELETION_CRITERIA, 1)
Remove all - db.COLLECTION_NAME.remove( {} );
* Projection
>db.COLLECTION_NAME.find({},{KEY:1})
eg: >db.mycol.find({},{"title":1,_id:0})
* Limit Records
limit() medthod
db.COLLECTION_NAME.find().limit(NUMBER);
skip() method
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) - default skip(0);
* Sort Records
db.COLLECTION_NAME.find().sort(NUMBER) NUMBER is -1 or 1 || 1 is ascending order(1,2,3), -1 is descending order(3,2,1) - default: ascending order
* Indexing
createIndex() method
db.COLLECTION_NAME.createIndex({KEY:1})
dropIndex() method
db.COLLECTION_NAME.dropIndex({KEY:1})
dropIndexes() method
>db.COLLECTION_NAME.dropIndexes()
getIndexes() method
db.COLLECTION_NAME.getIndexes()
* Aggregate() method
syntax: db.COLLECTION.aggregate(AGGREGARE_OPERATION )
eg: > db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
eg: db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
- $sum, $avg $min $max $push $addToSet $first $last
* Mongo Replication
* Mongo Sharding
* Mongo Create Backup
* Mongo Deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment