# MongoDB Cheatsheet **Published by:** [coinvest](https://paragraph.com/@coinvest/) **Published on:** 2023-09-23 **URL:** https://paragraph.com/@coinvest/mongodb-cheatsheet ## Content Here’s a cheat-sheet for basic database management and manipulation in MongoDB. Check for mongo process ps -ef | grep mongo Show DBs show dbs Create & Use DB use DATABASE_NAME Drop DB (must switch to it) db.dropDatabase() Check Current DB db Create Collection db.createCollection("COLLECTION_NAME") db.colectionName.insertOne({ keyvalue_1: value, keyValue_2: value, .............. }) Show Collections show collections Drop Collection db.COLLECTION_NAME.drop() Insert db.COLLECTION_NAME.insert(document) db.COLLECTION_NAME.insert({ _id: ObjectId(7df78ad8902c), title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) db.COLLECTION_NAME.insert([ { title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { title: 'NoSQL Database', description: 'NoSQL database doesn't have tables', by: 'tutorials point', url: 'http://www.tutorialspoint.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 20, comments: [ { user:'user1', message: 'My first comment', dateCreated: new Date(2013,11,10,2,35), like: 0 } ] } ]) db.COLLECTION_NAME.insertOne({key: "value"}) Find db.COLLECTION_NAME.find() db.COLLECTION_NAME.find({key1:value1, key2:value2}) Find without id: db.COLLECTION_NAME.find({}, {key1: value1, _id: 0}) Find one document: db.COLLECTION_NAME.findOne({key: value}) Pretty Response Find db.COLLECTION_NAME.find().pretty() db.COLLECTION_NAME.find({key1:value1, key2:value2}).pretty() Update db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA) db.COLLECTION_NAME.updateOne db.COLLECTION_NAME.updateMany db.COLLECTION_NAME.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}}) Save db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA}) db.COLLECTION_NAME.save( { "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point New Topic", "by":"Tutorials Point" } ) Delete collection: db.COLLECTION_NAME.deleteOne({ _id: ObjectId("documentID")}) db.COLLECTION_NAME.deleteMany({}) - will delete all documents in collection ## Publication Information - [coinvest](https://paragraph.com/@coinvest/): Publication homepage - [All Posts](https://paragraph.com/@coinvest/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@coinvest): Subscribe to updates ## Optional - [Collect as NFT](https://paragraph.com/@coinvest/mongodb-cheatsheet): Support the author by collecting this post - [View Collectors](https://paragraph.com/@coinvest/mongodb-cheatsheet/collectors): See who has collected this post