Create and drop Database in mongodb

Step 1: Open Mongo Shell

C:\Program Files\MongoDB\Server\4.2\bin>mongo

Step 2: To open existing db

> use order
switched to db order

Step 3: To create new db

> use mynewdb
switched to db mynewdb

Step 4: shows all existing dbs

> show dbs
Order   0.000GB
TestDb  0.000GB
admin   0.000GB
config  0.000GB
local   0.000GB

Step 5: Insert new row in mynewdb

> db.mynewdb.insertOne({name:"test"})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f0b8059bf0218ba46adc132")
}
> db.mynewdb.insertOne({name:"newtest"})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f0b8063bf0218ba46adc133")
}
> db.mynewdb.insertOne({name:"newtestwithid",id:111})
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5f0b8077bf0218ba46adc134")
}

Step 6: List all basic shell script operation by db.mynewdb. and press tab key


db.mynewdb.addIdIfNeeded(              db.mynewdb.getCollection(              db.mynewdb.propertyIsEnumerable
db.mynewdb.aggregate(                  db.mynewdb.getDB(                      db.mynewdb.prototype
db.mynewdb.bulkWrite(                  db.mynewdb.getFullName(                db.mynewdb.reIndex(
db.mynewdb.constructor                 db.mynewdb.getIndexKeys(               db.mynewdb.remove(
db.mynewdb.convertToCapped(            db.mynewdb.getIndexSpecs(              db.mynewdb.renameCollection(
db.mynewdb.convertToSingleObject(      db.mynewdb.getIndexes(                 db.mynewdb.replaceOne(
db.mynewdb.count(                      db.mynewdb.getIndices(                 db.mynewdb.runCommand(
db.mynewdb.countDocuments(             db.mynewdb.getMongo(                   db.mynewdb.runReadCommand(
db.mynewdb.createIndex(                db.mynewdb.getName(                    db.mynewdb.save(
db.mynewdb.createIndexes(              db.mynewdb.getPlanCache(               db.mynewdb.setSlaveOk(
db.mynewdb.dataSize(                   db.mynewdb.getQueryOptions(            db.mynewdb.setWriteConcern(
db.mynewdb.deleteMany(                 db.mynewdb.getShardDistribution(       db.mynewdb.shellPrint(
db.mynewdb.deleteOne(                  db.mynewdb.getShardVersion(            db.mynewdb.stats(
db.mynewdb.distinct(                   db.mynewdb.getSlaveOk(                 db.mynewdb.storageSize(
db.mynewdb.drop(                       db.mynewdb.getSplitKeysForChunks(      db.mynewdb.toLocaleString
db.mynewdb.dropIndex(                  db.mynewdb.getWriteConcern(            db.mynewdb.toString(
db.mynewdb.dropIndexes(                db.mynewdb.hasOwnProperty              db.mynewdb.tojson(
db.mynewdb.ensureIndex(                db.mynewdb.hashAllDocs(                db.mynewdb.totalIndexSize(
db.mynewdb.estimatedDocumentCount(     db.mynewdb.help(                       db.mynewdb.totalSize(
db.mynewdb.exists(                     db.mynewdb.initializeOrderedBulkOp(    db.mynewdb.unsetWriteConcern(
db.mynewdb.explain(                    db.mynewdb.initializeUnorderedBulkOp(  db.mynewdb.update(
db.mynewdb.find(                       db.mynewdb.insert(                     db.mynewdb.updateMany(
db.mynewdb.findAndModify(              db.mynewdb.insertMany(                 db.mynewdb.updateOne(
db.mynewdb.findOne(                    db.mynewdb.insertOne(                  db.mynewdb.validate(
db.mynewdb.findOneAndDelete(           db.mynewdb.isCapped(                   db.mynewdb.valueOf(
db.mynewdb.findOneAndReplace(          db.mynewdb.latencyStats(               db.mynewdb.verify(
db.mynewdb.findOneAndUpdate(           db.mynewdb.mapReduce(                  db.mynewdb.watch(

Step 7: Find mynewdb data

> db.mynewdb.find()
{ "_id" : ObjectId("5f0b8059bf0218ba46adc132"), "name" : "test" }
{ "_id" : ObjectId("5f0b8063bf0218ba46adc133"), "name" : "newtest" }
{ "_id" : ObjectId("5f0b8077bf0218ba46adc134"), "name" : "newtestwithid", "id" : 111 }

Step 8: Find mynewdb data by id

> db.mynewdb.find({id:111})
{ "_id" : ObjectId("5f0b8077bf0218ba46adc134"), "name" : "newtestwithid", "id" : 111 }

Step 9: Find my mynewdb data by name

> db.mynewdb.find({name:"test"})
{ "_id" : ObjectId("5f0b8059bf0218ba46adc132"), "name" : "test" }

Step 10: Drop mynewdb

> db.mynewdb.drop()
true

Step 11: Verify db deleted or not

> show dbs
Order   0.000GB
TestDb  0.000GB
admin   0.000GB
config  0.000GB
local   0.000GB

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *