Contents hide 1) Table Of Content 2) Create MongoDB Database 2.1) The use Command 2.2) The createCollection() Method In MongoDB 3) Drop MongoDB Database 3.1) The dropDatabase() Method 3.2) The drop() Method In MongoDB 3.3) The insert() Method In MongoDB 3.3.1) Passing Array of document 4) MongoDB Update() Method 5) The remove() Method in MongoDB 5.1) Remove Only One 5.2) Remove All Documents Let Us see how to Perform simple MongoDB Queries such as create, insert, update, delete, drop Table Of Content Create Create Database Create Collection Drop Drop Database Drop Collection Insert Document Update Document Delete Document In this section, we will see how to create a database and collection in MongoDB. Create MongoDB Database The use Command MongoDB use DATABASE_NAME is used to make a database. The command will create a new database if it does not exist, otherwise, it’ll return the present database. Syntax For Creating New Database The Basic syntax of use database statement is as follows – MongoDB use DATABASE_NAME for Example, if you want to use the database names as `movie_collection` then the following statement will be used MongoDB >use movie_collection switched to db movie_collection If you want to check which database is been currently used. you can use this following statement – MongoDB >db movie_collection and if you want to check all the list of the database, you can use this following command MongoDB >show dbs local 0.78125GB test 0.23012GB Your created database (movie_collection) is not present in the list. To display database, you need to insert at least one document into it. MongoDB >db.movie.insert({"name":"Crypto"}) >show dbs local 0.78125GB mydb 0.23012GB test 0.23012GB The createCollection() Method In MongoDB for creating a collection in MongoDB the method createCollection(name, options) is used. For Example MongoDB db.createCollection(name, options) In the above method, the name is the name of collection which needs to b created. where else option is the document and is used to specify the configuration of a collection. Parameter Type Description Name String Name of the collection Options Document (Optional) Specify options about memory size and indexing Following table shows the list of options you can use in method createCollection(name, options) . Field Type Description max number (Optional) Specifies the maximum number of documents allowed within the capped collection. size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you would like to specify this field additionally. autoIndexId Boolean (Optional) If true, automatically create an index on child field.s Default value is false. capped Boolean (Optional) If true, allows a capped collection. A capped collection is a mounted size collection that automatically overwrites its oldest entries once it reaches its most size. If you specify true, you wish to specify size parameter additionally. MongoDB initial checks size field of capped collection, then it checks max field while inserting the document Examples The basic syntax of the method createCollection() without options is as follows − MongoDB >use movie_collection switched to db movie_collection >db.createCollection("popular_movies") { "ok" : 1 } The method createCollection() without options is as follows − MongoDB >db.createCollection("mycol", { capped:true, autoIndexId:true, size:6142800, max:10000 } ) { "ok" : 1 } > In this section, we will see how to drop a database and collection in MongoDB. Drop MongoDB Database The dropDatabase() Method To drop an existing database in MongoDB db.dropDatabase() method is used. Syntax – MongoDB db.dropDatabase() The above method will delete the selected database. If you haven’t selected any database, then it will delete the default ‘test’ database. The drop() Method In MongoDB To drop an existing collection in MongoDB db.collection.drop() method is used. Syntax – MongoDB >db.movie_collection.drop() true drop() method will return true, if the selected collection is dropped successfully, otherwise it will return false. In this section, we will see how to insert a document in MongoDB. The insert() Method In MongoDB We can use insert() or save() method to insert data into MongoDB collection Example – MongoDB >db.mycol.insert({ _id: ObjectId(7df78ad8902c), title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'tutorials point', url: 'https://www.geekstrick.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }) Here mycol is our collection name, as created in the previous chapter. If the collection does not exist within the database, then MongoDB can create this collection and then insert a document into it. In the inserted document, if we do not specify the aid parameter, then MongoDB assigns a unique ObjectId for this document. _id is twelve bytes hex number unique for every document in a collection. twelve bytes are divided as follows Encoding _id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer) Passing Array of document To insert multiple documents in a single query, you can pass an array of documents in insert() command. Example – MongoDB >db.post.insert([ { title: 'MongoDB Overview', description: 'MongoDB is no sql database', by: 'tutorials point', url: 'https://www.geekstrick.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { title: 'NoSQL Database', description: "NoSQL database doesn't have tables", by: 'tutorials point', url: 'https://www.geekstrick.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 20, comments: [ { user:'user1', message: 'Its a comment', dateCreated: new Date(2013,11,10,2,35), like: 0 } ] } ]) To insert the document you’ll be able to use db.post.save(document) also. If you do not specify child in the document then save() method will work same as insert() method. If you specify child then it will replace whole data of document containing child as specified in save() method. MongoDB Update() Method MongoDB’s update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method. Example Consider the mycol collection has the following data. MongoDB { "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} Following example will set the new title ‘New MongoDB Tutorial’ of the documents whose title is ‘MongoDB Overview’. MongoDB >db.mycol.update({'title':'MongoDB Overview'},{$set:{'title':'New MongoDB Tutorial'}}) >db.mycol.find() { "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"} { "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"} > To update multiple documents, you need to set a parameter ‘multi’ to true. MongoDB >db.mycol.update({'title':'MongoDB Overview'}, {$set:{'title':'New MongoDB Tutorial'}},{multi:true}) The remove() Method in MongoDB In this section we will see how to delete a document using MongoDB. MongoDB’s remove() method is used to remove a document from the collection. remove() method accepts 2 parameters. One is deletion criteria and second is justOne flag. deletion criteria – (Optional) deletion criteria according to documents will be removed. justOne – (Optional) if set to true or 1, then remove only one document. Remove Only One If there are multiple records and you want to delete only the first record, then set justOne parameter in remove() method. MongoDB >db.COLLECTION_NAME.remove(DELETION_CRITERIA,1) Remove All Documents If you don’t specify deletion criteria, then MongoDB will delete whole documents from the collection. This is equivalent of SQL’s truncate command. MongoDB >db.mycol.remove() >db.mycol.find() > Get More Articles On MongoDB Share this:TwitterFacebookRedditLinkedInWhatsAppPrintTumblr Related Tags: Collection, createCollection(), CRUD Queries, MongoDB, MongoDB Queries, NoSQL