MongoDB – Discussion

Discuss MongoDB ”; Previous Next MongoDB is an open-source document database and leading NoSQL database. MongoDB is written in C++. This tutorial will give you great understanding on MongoDB concepts needed to create and deploy a highly scalable and performance-oriented database. Print Page Previous Next Advertisements ”;

MongoDB – Quick Guide

MongoDB – Quick Guide ”; Previous Next MongoDB – Overview MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. Database Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases. Collection Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose. Document A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection”s documents may hold different types of data. The following table shows the relationship of RDBMS terminology with MongoDB. RDBMS MongoDB Database Database Table Collection Tuple/Row Document column Field Table Join Embedded Documents Primary Key Primary Key (Default key _id provided by mongodb itself) Database Server and Client Mysqld/Oracle mongod mysql/sqlplus mongo Sample Document Following example shows the document structure of a blog site, which is simply a comma separated key value pair. { _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, comments: [ { user:”user1”, message: ”My first comment”, dateCreated: new Date(2011,1,20,2,15), like: 0 }, { user:”user2”, message: ”My second comments”, dateCreated: new Date(2011,1,25,7,45), like: 5 } ] } _id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. If you don’t provide then MongoDB provides a unique id for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of MongoDB server and remaining 3 bytes are simple incremental VALUE. MongoDB – Advantages Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB, there is no concept of relationship. Advantages of MongoDB over RDBMS Schema less − MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another. Structure of a single object is clear. No complex joins. Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that”s nearly as powerful as SQL. Tuning. Ease of scale-out − MongoDB is easy to scale. Conversion/mapping of application objects to database objects not needed. Uses internal memory for storing the (windowed) working set, enabling faster access of data. Why Use MongoDB? Document Oriented Storage − Data is stored in the form of JSON style documents. Index on any attribute Replication and high availability Auto-sharding Rich queries Fast in-place updates Professional support by MongoDB Where to Use MongoDB? Big Data Content Management and Delivery Mobile and Social Infrastructure User Data Management Data Hub MongoDB – Environment Let us now see how to install MongoDB on Windows. Install MongoDB On Windows To install MongoDB on Windows, first download the latest release of MongoDB from https://www.mongodb.org/downloads. Make sure you get correct version of MongoDB depending upon your Windows version. To get your Windows version, open command prompt and execute the following command. C:>wmic os get osarchitecture OSArchitecture 64-bit C:> 32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and evaluation purposes. Now extract your downloaded file to c: drive or any other location. Make sure the name of the extracted folder is mongodb-win32-i386-[version] or mongodb-win32-x86_64-[version]. Here [version] is the version of MongoDB download. Next, open the command prompt and run the following command. C:>move mongodb-win64-* mongodb 1 dir(s) moved. C:> In case you have extracted the MongoDB at different location, then go to that path by using command cd FOLDER/DIR and now run the above given process. MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is c:datadb. So you need to create this folder using the Command Prompt. Execute the following command sequence. C:>md data C:md datadb If you have to install the MongoDB at a different location, then you need to specify an alternate path for datadb by setting the path dbpath in mongod.exe. For the same, issue the following commands. In the command prompt, navigate to the bin directory present in the MongoDB installation folder. Suppose my installation folder is D:set upmongodb C:UsersXYZ>d: D:>cd “set up” D:set up>cd mongodb D:set upmongodb>cd bin D:set upmongodbbin>mongod.exe –dbpath “d:set upmongodbdata” This will show waiting for connections message on the console output, which indicates that the mongod.exe process is running successfully. Now to run the MongoDB, you need to open another command prompt and issue the following command. D:set upmongodbbin>mongo.exe MongoDB shell version: 2.4.6 connecting to: test >db.test.save( { a: 1 } ) >db.test.find() { “_id” : ObjectId(5879b0f65a56a454), “a” : 1 } > This will show that MongoDB is installed and run successfully. Next time when you run MongoDB, you need to issue only commands. D:set upmongodbbin>mongod.exe –dbpath “d:set upmongodbdata” D:set upmongodbbin>mongo.exe Install MongoDB on Ubuntu Run the following command to import the MongoDB public GPG key − sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10 Create a /etc/apt/sources.list.d/mongodb.list file using the following command. echo ”deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen” | sudo tee /etc/apt/sources.list.d/mongodb.list Now issue the following command to update the repository − sudo apt-get update Next install the MongoDB by using the following command − apt-get install mongodb-10gen = 2.2.3 In the above installation, 2.2.3 is currently released MongoDB version. Make sure to install the latest version always. Now MongoDB is installed successfully. Start MongoDB sudo service mongodb start Stop MongoDB sudo service mongodb stop Restart MongoDB sudo service mongodb restart To use

MongoDB – Useful Resources

MongoDB – Useful Resources ”; Previous Next The following resources contain additional information on MongoDB. Please use them to get more in-depth knowledge on this topic. Useful Video Courses MongoDB Online Training Best Seller 45 Lectures 3 hours Tutorialspoint More Detail MongoDB Course For Absolute Beginners Best Seller 55 Lectures 5.5 hours Eduonix Learning Solutions More Detail Real-world App with ASP.NET CORE 3.1 MVC & MongoDB (NoSQL) Best Seller 41 Lectures 2.5 hours University Code More Detail Mongo DB Course: Learn all about MongoDB Best Seller 71 Lectures 2.5 hours Skillbakery More Detail Introduction to MongoDB Administration Featured 41 Lectures 4.5 hours Shruchin Varma Boreda More Detail MongoDB—Mastering MongoDB for Beginners (Theory and Projects) 155 Lectures 10.5 hours Packt Publishing More Detail Print Page Previous Next Advertisements ”;

MongoDB – ObjectId

MongoDB – ObjectId ”; Previous Next We have been using MongoDB Object Id in all the previous chapters. In this chapter, we will understand the structure of ObjectId. An ObjectId is a 12-byte BSON type having the following structure − The first 4 bytes representing the seconds since the unix epoch The next 3 bytes are the machine identifier The next 2 bytes consists of process id The last 3 bytes are a random counter value MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the creation of any document. The complex combination of ObjectId makes all the _id fields unique. Creating New ObjectId To generate a new ObjectId use the following code − >newObjectId = ObjectId() The above statement returned the following uniquely generated id − ObjectId(“5349b4ddd2781d08c09890f3”) Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id − >myObjectId = ObjectId(“5349b4ddd2781d08c09890f4”) Creating Timestamp of a Document Since the _id ObjectId by default stores the 4-byte timestamp, in most cases you do not need to store the creation time of any document. You can fetch the creation time of a document using getTimestamp method − >ObjectId(“5349b4ddd2781d08c09890f4”).getTimestamp() This will return the creation time of this document in ISO date format − ISODate(“2014-04-12T21:49:17Z”) Converting ObjectId to String In some cases, you may need the value of ObjectId in a string format. To convert the ObjectId in string, use the following code − >newObjectId.str The above code will return the string format of the Guid − 5349b4ddd2781d08c09890f3 Print Page Previous Next Advertisements ”;

MongoDB – Text Search

MongoDB – Text Search ”; Previous Next Starting from version 2.4, MongoDB started supporting text indexes to search inside string content. The Text Search uses stemming techniques to look for specified words in the string fields by dropping stemming stop words like a, an, the, etc. At present, MongoDB supports around 15 languages. Enabling Text Search Initially, Text Search was an experimental feature but starting from version 2.6, the configuration is enabled by default. Creating Text Index Consider the following document under posts collection containing the post text and its tags − > db.posts.insert({ “post_text”: “enjoy the mongodb articles on tutorialspoint”, “tags”: [“mongodb”, “tutorialspoint”] } { “post_text” : “writing tutorials on mongodb”, “tags” : [ “mongodb”, “tutorial” ] }) WriteResult({ “nInserted” : 1 }) We will create a text index on post_text field so that we can search inside our posts” text − >db.posts.createIndex({post_text:”text”}) { “createdCollectionAutomatically” : true, “numIndexesBefore” : 1, “numIndexesAfter” : 2, “ok” : 1 } Using Text Index Now that we have created the text index on post_text field, we will search for all the posts having the word tutorialspoint in their text. > db.posts.find({$text:{$search:”tutorialspoint”}}).pretty() { “_id” : ObjectId(“5dd7ce28f1dd4583e7103fe0”), “post_text” : “enjoy the mongodb articles on tutorialspoint”, “tags” : [ “mongodb”, “tutorialspoint” ] } The above command returned the following result documents having the word tutorialspoint in their post text − { “_id” : ObjectId(“53493d14d852429c10000002”), “post_text” : “enjoy the mongodb articles on tutorialspoint”, “tags” : [ “mongodb”, “tutorialspoint” ] } Deleting Text Index To delete an existing text index, first find the name of index using the following query − >db.posts.getIndexes() [ { “v” : 2, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “mydb.posts” }, { “v” : 2, “key” : { “fts” : “text”, “ftsx” : 1 }, “name” : “post_text_text”, “ns” : “mydb.posts”, “weights” : { “post_text” : 1 }, “default_language” : “english”, “language_override” : “language”, “textIndexVersion” : 3 } ] > After getting the name of your index from above query, run the following command. Here, post_text_text is the name of the index. >db.posts.dropIndex(“post_text_text”) { “nIndexesWas” : 2, “ok” : 1 } Print Page Previous Next Advertisements ”;

MongoDB – Atomic Operations

MongoDB – Atomic Operations ”; Previous Next Model Data for Atomic Operations The recommended approach to maintain atomicity would be to keep all the related information, which is frequently updated together in a single document using embedded documents. This would make sure that all the updates for a single document are atomic. Assume we have created a collection with name productDetails and inserted a documents in it as shown below − >db.createCollection(“products”) { “ok” : 1 } > db.productDetails.insert( { “_id”:1, “product_name”: “Samsung S3”, “category”: “mobiles”, “product_total”: 5, “product_available”: 3, “product_bought_by”: [ { “customer”: “john”, “date”: “7-Jan-2014” }, { “customer”: “mark”, “date”: “8-Jan-2014” } ] } ) WriteResult({ “nInserted” : 1 }) > In this document, we have embedded the information of the customer who buys the product in the product_bought_by field. Now, whenever a new customer buys the product, we will first check if the product is still available using product_available field. If available, we will reduce the value of product_available field as well as insert the new customer”s embedded document in the product_bought_by field. We will use findAndModify command for this functionality because it searches and updates the document in the same go. >db.products.findAndModify({ query:{_id:2,product_available:{$gt:0}}, update:{ $inc:{product_available:-1}, $push:{product_bought_by:{customer:”rob”,date:”9-Jan-2014″}} } }) Our approach of embedded document and using findAndModify query makes sure that the product purchase information is updated only if it the product is available. And the whole of this transaction being in the same query, is atomic. In contrast to this, consider the scenario where we may have kept the product availability and the information on who has bought the product, separately. In this case, we will first check if the product is available using the first query. Then in the second query we will update the purchase information. However, it is possible that between the executions of these two queries, some other user has purchased the product and it is no more available. Without knowing this, our second query will update the purchase information based on the result of our first query. This will make the database inconsistent because we have sold a product which is not available. Print Page Previous Next Advertisements ”;

MongoDB – GridFS

MongoDB – GridFS ”; Previous Next GridFS is the MongoDB specification for storing and retrieving large files such as images, audio files, video files, etc. It is kind of a file system to store files but its data is stored within MongoDB collections. GridFS has the capability to store files even greater than its document size limit of 16MB. GridFS divides a file into chunks and stores each chunk of data in a separate document, each of maximum size 255k. GridFS by default uses two collections fs.files and fs.chunks to store the file”s metadata and the chunks. Each chunk is identified by its unique _id ObjectId field. The fs.files serves as a parent document. The files_id field in the fs.chunks document links the chunk to its parent. Following is a sample document of fs.files collection − { “filename”: “test.txt”, “chunkSize”: NumberInt(261120), “uploadDate”: ISODate(“2014-04-13T11:32:33.557Z”), “md5”: “7b762939321e146569b07f72c62cca4f”, “length”: NumberInt(646) } The document specifies the file name, chunk size, uploaded date, and length. Following is a sample document of fs.chunks document − { “files_id”: ObjectId(“534a75d19f54bfec8a2fe44b”), “n”: NumberInt(0), “data”: “Mongo Binary Data” } Adding Files to GridFS Now, we will store an mp3 file using GridFS using the put command. For this, we will use the mongofiles.exe utility present in the bin folder of the MongoDB installation folder. Open your command prompt, navigate to the mongofiles.exe in the bin folder of MongoDB installation folder and type the following code − >mongofiles.exe -d gridfs put song.mp3 Here, gridfs is the name of the database in which the file will be stored. If the database is not present, MongoDB will automatically create a new document on the fly. Song.mp3 is the name of the file uploaded. To see the file”s document in database, you can use find query − >db.fs.files.find() The above command returned the following document − { _id: ObjectId(”534a811bf8b4aa4d33fdf94d”), filename: “song.mp3”, chunkSize: 261120, uploadDate: new Date(1397391643474), md5: “e4f53379c909f7bed2e9d631e15c1c41″, length: 10401959 } We can also see all the chunks present in fs.chunks collection related to the stored file with the following code, using the document id returned in the previous query − >db.fs.chunks.find({files_id:ObjectId(”534a811bf8b4aa4d33fdf94d”)}) In my case, the query returned 40 documents meaning that the whole mp3 document was divided in 40 chunks of data. Print Page Previous Next Advertisements ”;

Working with Rockmongo

Working with RockMongo ”; Previous Next RockMongo is a MongoDB administration tool using which you can manage your server, databases, collections, documents, indexes, and a lot more. It provides a very user-friendly way for reading, writing, and creating documents. It is similar to PHPMyAdmin tool for PHP and MySQL. Downloading RockMongo You can download the latest version of RockMongo from here: https://github.com/iwind/rockmongo Installing RockMongo Once downloaded, you can unzip the package in your server root folder and rename the extracted folder to rockmongo. Open any web browser and access the index.php page from the folder rockmongo. Enter admin/admin as username/password respectively. Working with RockMongo We will now be looking at some basic operations that you can perform with RockMongo. Creating New Database To create a new database, click Databases tab. Click Create New Database. On the next screen, provide the name of the new database and click on Create. You will see a new database getting added in the left panel. Creating New Collection To create a new collection inside a database, click on that database from the left panel. Click on the New Collection link on top. Provide the required name of the collection. Do not worry about the other fields of Is Capped, Size and Max. Click on Create. A new collection will be created and you will be able to see it in the left panel. Creating New Document To create a new document, click on the collection under which you want to add documents. When you click on a collection, you will be able to see all the documents within that collection listed there. To create a new document, click on the Insert link at the top. You can enter the document”s data either in JSON or array format and click on Save. Export/Import Data To import/export data of any collection, click on that collection and then click on Export/Import link on the top panel. Follow the next instructions to export your data in a zip format and then import the same zip file to import back data. Print Page Previous Next Advertisements ”;

Auto-Increment Sequence

MongoDB – Auto-Increment Sequence ”; Previous Next MongoDB does not have out-of-the-box auto-increment functionality, like SQL databases. By default, it uses the 12-byte ObjectId for the _id field as the primary key to uniquely identify the documents. However, there may be scenarios where we may want the _id field to have some auto-incremented value other than the ObjectId. Since this is not a default feature in MongoDB, we will programmatically achieve this functionality by using a counters collection as suggested by the MongoDB documentation. Using Counter Collection Consider the following products document. We want the _id field to be an auto-incremented integer sequence starting from 1,2,3,4 upto n. { “_id”:1, “product_name”: “Apple iPhone”, “category”: “mobiles” } For this, create a counters collection, which will keep track of the last sequence value for all the sequence fields. >db.createCollection(“counters”) Now, we will insert the following document in the counters collection with productid as its key − > db.counters.insert({ “_id”:”productid”, “sequence_value”: 0 }) WriteResult({ “nInserted” : 1 }) > The field sequence_value keeps track of the last value of the sequence. Use the following code to insert this sequence document in the counters collection − >db.counters.insert({_id:”productid”,sequence_value:0}) Creating Javascript Function Now, we will create a function getNextSequenceValue which will take the sequence name as its input, increment the sequence number by 1 and return the updated sequence number. In our case, the sequence name is productid. >function getNextSequenceValue(sequenceName){ var sequenceDocument = db.counters.findAndModify({ query:{_id: sequenceName }, update: {$inc:{sequence_value:1}}, new:true }); return sequenceDocument.sequence_value; } Using the Javascript Function We will now use the function getNextSequenceValue while creating a new document and assigning the returned sequence value as document”s _id field. Insert two sample documents using the following code − >db.products.insert({ “_id”:getNextSequenceValue(“productid”), “product_name”:”Apple iPhone”, “category”:”mobiles” }) >db.products.insert({ “_id”:getNextSequenceValue(“productid”), “product_name”:”Samsung S3″, “category”:”mobiles” }) As you can see, we have used the getNextSequenceValue function to set value for the _id field. To verify the functionality, let us fetch the documents using find command − >db.products.find() The above query returned the following documents having the auto-incremented _id field − { “_id” : 1, “product_name” : “Apple iPhone”, “category” : “mobiles”} { “_id” : 2, “product_name” : “Samsung S3”, “category” : “mobiles” } Print Page Previous Next Advertisements ”;

MongoDB – Indexing Limitations

MongoDB – Indexing Limitations ”; Previous Next In this chapter, we will learn about Indexing Limitations and its other components. Extra Overhead Every index occupies some space as well as causes an overhead on each insert, update and delete. So if you rarely use your collection for read operations, it makes sense not to use indexes. RAM Usage Since indexes are stored in RAM, you should make sure that the total size of the index does not exceed the RAM limit. If the total size increases the RAM size, it will start deleting some indexes, causing performance loss. Query Limitations Indexing can”t be used in queries which use − Regular expressions or negation operators like $nin, $not, etc. Arithmetic operators like $mod, etc. $where clause Hence, it is always advisable to check the index usage for your queries. Index Key Limits Starting from version 2.6, MongoDB will not create an index if the value of existing index field exceeds the index key limit. Inserting Documents Exceeding Index Key Limit MongoDB will not insert any document into an indexed collection if the indexed field value of this document exceeds the index key limit. Same is the case with mongorestore and mongoimport utilities. Maximum Ranges A collection cannot have more than 64 indexes. The length of the index name cannot be longer than 125 characters. A compound index can have maximum 31 fields indexed. Print Page Previous Next Advertisements ”;