Category: mongodb

  • Khóa học miễn phí MongoDB – Indexing nhận dự án làm có lương

    MongoDB – Indexing



    Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and require MongoDB to process a large volume of data.

    Indexes are special data structures, that store a small portion of the data set in an easy-to-traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in the index.

    The createIndex() Method

    To create an index, you need to use createIndex() method of MongoDB.

    Syntax

    The basic syntax of createIndex() method is as follows().

    >db.COLLECTION_NAME.createIndex({KEY:1})
    

    Here key is the name of the field on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1.

    Example

    >db.mycol.createIndex({"title":1})
    {
    	"createdCollectionAutomatically" : false,
    	"numIndexesBefore" : 1,
    	"numIndexesAfter" : 2,
    	"ok" : 1
    }
    >
    

    In createIndex() method you can pass multiple fields, to create index on multiple fields.

    >db.mycol.createIndex({"title":1,"description":-1})
    >
    

    This method also accepts list of options (which are optional). Following is the list −

    Parameter Type Description
    background Boolean Builds the index in the background so that building an index does not block other database activities. Specify true to build in the background. The default value is false.
    unique Boolean Creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing value in the index. Specify true to create a unique index. The default value is false.
    name string The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order.
    sparse Boolean If true, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). The default value is false.
    expireAfterSeconds integer Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection.
    weights document The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score.
    default_language string For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. The default value is English.
    language_override string For a text index, specify the name of the field in the document that contains, the language to override the default language. The default value is language.

    The dropIndex() method

    You can drop a particular index using the dropIndex() method of MongoDB.

    Syntax

    The basic syntax of DropIndex() method is as follows().

    >db.COLLECTION_NAME.dropIndex({KEY:1})
    

    Here, “key” is the name of the file on which you want to remove an existing index. Instead of the index specification document (above syntax), you can also specify the name of the index directly as:

    dropIndex("name_of_the_index")
    

    Example

    > db.mycol.dropIndex({"title":1})
    {
    	"ok" : 0,
    	"errmsg" : "can''t find index with key: { title: 1.0 }",
    	"code" : 27,
    	"codeName" : "IndexNotFound"
    }
    

    The dropIndexes() method

    This method deletes multiple (specified) indexes on a collection.

    Syntax

    The basic syntax of DropIndexes() method is as follows() −

    >db.COLLECTION_NAME.dropIndexes()
    

    Example

    Assume we have created 2 indexes in the named mycol collection as shown below −

    > db.mycol.createIndex({"title":1,"description":-1})
    

    Following example removes the above created indexes of mycol −

    >db.mycol.dropIndexes({"title":1,"description":-1})
    { "nIndexesWas" : 2, "ok" : 1 }
    >
    

    The getIndexes() method

    This method returns the description of all the indexes int the collection.

    Syntax

    Following is the basic syntax od the getIndexes() method −

    db.COLLECTION_NAME.getIndexes()
    

    Example

    Assume we have created 2 indexes in the named mycol collection as shown below −

    > db.mycol.createIndex({"title":1,"description":-1})
    

    Following example retrieves all the indexes in the collection mycol −

    > db.mycol.getIndexes()
    [
    	{
    		"v" : 2,
    		"key" : {
    			"_id" : 1
    		},
    		"name" : "_id_",
    		"ns" : "test.mycol"
    	},
    	{
    		"v" : 2,
    		"key" : {
    			"title" : 1,
    			"description" : -1
    		},
    		"name" : "title_1_description_-1",
    		"ns" : "test.mycol"
    	}
    ]
    >
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Sharding nhận dự án làm có lương

    MongoDB – Sharding



    Sharding is the process of storing data records across multiple machines and it is MongoDB”s approach to meeting the demands of data growth. As the size of the data increases, a single machine may not be sufficient to store the data nor provide an acceptable read and write throughput. Sharding solves the problem with horizontal scaling. With sharding, you add more machines to support data growth and the demands of read and write operations.

    Why Sharding?

    • In replication, all writes go to master node

    • Latency sensitive queries still go to master

    • Single replica set has limitation of 12 nodes

    • Memory can”t be large enough when active dataset is big

    • Local disk is not big enough

    • Vertical scaling is too expensive

    Sharding in MongoDB

    The following diagram shows the Sharding in MongoDB using sharded cluster.

    MongoDB Sharding

    In the following diagram, there are three main components −

    • Shards − Shards are used to store data. They provide high availability and data consistency. In production environment, each shard is a separate replica set.

    • Config Servers − Config servers store the cluster”s metadata. This data contains a mapping of the cluster”s data set to the shards. The query router uses this metadata to target operations to specific shards. In production environment, sharded clusters have exactly 3 config servers.

    • Query Routers − Query routers are basically mongo instances, interface with client applications and direct operations to the appropriate shard. The query router processes and targets the operations to shards and then returns results to the clients. A sharded cluster can contain more than one query router to divide the client request load. A client sends requests to one query router. Generally, a sharded cluster have many query routers.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Deployment nhận dự án làm có lương

    MongoDB – Deployment



    When you are preparing a MongoDB deployment, you should try to understand how your application is going to hold up in production. It’s a good idea to develop a consistent, repeatable approach to managing your deployment environment so that you can minimize any surprises once you’re in production.

    The best approach incorporates prototyping your set up, conducting load testing, monitoring key metrics, and using that information to scale your set up. The key part of the approach is to proactively monitor your entire system – this will help you understand how your production system will hold up before deploying, and determine where you will need to add capacity. Having insight into potential spikes in your memory usage, for example, could help put out a write-lock fire before it starts.

    To monitor your deployment, MongoDB provides some of the following commands −

    mongostat

    This command checks the status of all running mongod instances and return counters of database operations. These counters include inserts, queries, updates, deletes, and cursors. Command also shows when you’re hitting page faults, and showcase your lock percentage. This means that you”re running low on memory, hitting write capacity or have some performance issue.

    To run the command, start your mongod instance. In another command prompt, go to bin directory of your mongodb installation and type mongostat.

    D:set upmongodbbin>mongostat
    

    Following is the output of the command −

    mongostat mongostat2

    mongotop

    This command tracks and reports the read and write activity of MongoDB instance on a collection basis. By default, mongotop returns information in each second, which you can change it accordingly. You should check that this read and write activity matches your application intention, and you’re not firing too many writes to the database at a time, reading too frequently from a disk, or are exceeding your working set size.

    To run the command, start your mongod instance. In another command prompt, go to bin directory of your mongodb installation and type mongotop.

    D:set upmongodbbin>mongotop
    

    Following is the output of the command −

    mongotop mongotop2

    To change mongotop command to return information less frequently, specify a specific number after the mongotop command.

    D:set upmongodbbin>mongotop 30
    

    The above example will return values every 30 seconds.

    Apart from the MongoDB tools, 10gen provides a free, hosted monitoring service, MongoDB Management Service (MMS), that provides a dashboard and gives you a view of the metrics from your entire cluster.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Create Backup nhận dự án làm có lương

    MongoDB – Create Backup



    In this chapter, we will see how to create a backup in MongoDB.

    Dump MongoDB Data

    To create backup of database in MongoDB, you should use mongodump command. This command will dump the entire data of your server into the dump directory. There are many options available by which you can limit the amount of data or create backup of your remote server.

    Syntax

    The basic syntax of mongodump command is as follows −

    >mongodump
    

    Example

    Start your mongod server. Assuming that your mongod server is running on the localhost and port 27017, open a command prompt and go to the bin directory of your mongodb instance and type the command mongodump

    Consider the mycol collection has the following data.

    >mongodump
    

    The command will connect to the server running at 127.0.0.1 and port 27017 and back all data of the server to directory /bin/dump/. Following is the output of the command −

    DB Stats

    Following is a list of available options that can be used with the mongodump command.

    Syntax Description Example
    mongodump –host HOST_NAME –port PORT_NUMBER This commmand will backup all databases of specified mongod instance. mongodump –host tutorialspoint.com –port 27017
    mongodump –dbpath DB_PATH –out BACKUP_DIRECTORY This command will backup only specified database at specified path. mongodump –dbpath /data/db/ –out /data/backup/
    mongodump –collection COLLECTION –db DB_NAME This command will backup only specified collection of specified database. mongodump –collection mycol –db test

    Restore data

    To restore backup data MongoDB”s mongorestore command is used. This command restores all of the data from the backup directory.

    Syntax

    The basic syntax of mongorestore command is −

    >mongorestore
    

    Following is the output of the command −

    DB Stats

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Create Collection nhận dự án làm có lương

    MongoDB – Create Collection



    In this chapter, we will see how to create a collection using MongoDB.

    The createCollection() Method

    MongoDB db.createCollection(name, options) is used to create collection.

    Syntax

    Basic syntax of createCollection() command is as follows −

    db.createCollection(name, options)
    

    In the command, name is name of collection to be created. Options is a document and is used to specify configuration of collection.

    Parameter Type Description
    Name String Name of the collection to be created
    Options Document (Optional) Specify options about memory size and indexing

    Options parameter is optional, so you need to specify only the name of the collection. Following is the list of options you can use −

    Field Type Description
    capped Boolean (Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also.
    autoIndexId Boolean (Optional) If true, automatically create index on _id field.s Default value is false.
    size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also.
    max number (Optional) Specifies the maximum number of documents allowed in the capped collection.

    While inserting the document, MongoDB first checks size field of capped collection, then it checks max field.

    Examples

    Basic syntax of createCollection() method without options is as follows −

    >use test
    switched to db test
    >db.createCollection("mycollection")
    { "ok" : 1 }
    >
    

    You can check the created collection by using the command show collections.

    >show collections
    mycollection
    system.indexes
    

    The following example shows the syntax of createCollection() method with few important options −

    > db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ){
    "ok" : 0,
    "errmsg" : "BSON field ''create.autoIndexID'' is an unknown field.",
    "code" : 40415,
    "codeName" : "Location40415"
    }
    >
    

    In MongoDB, you don”t need to create collection. MongoDB creates collection automatically, when you insert some document.

    >db.tutorialspoint.insert({"name" : "tutorialspoint"}),
    WriteResult({ "nInserted" : 1 })
    >show collections
    mycol
    mycollection
    system.indexes
    tutorialspoint
    >
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Create Database nhận dự án làm có lương

    MongoDB – Create Database



    In this chapter, we will see how to create a database in MongoDB.

    The use Command

    MongoDB use DATABASE_NAME is used to create database. The command will create a new database if it doesn”t exist, otherwise it will return the existing database.

    Syntax

    Basic syntax of use DATABASE statement is as follows −

    use DATABASE_NAME
    

    Example

    If you want to use a database with name <mydb>, then use DATABASE statement would be as follows −

    >use mydb
    switched to db mydb
    

    To check your currently selected database, use the command db

    >db
    mydb
    

    If you want to check your databases list, use the command show dbs.

    >show dbs
    local     0.78125GB
    test      0.23012GB
    

    Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it.

    >db.movie.insert({"name":"tutorials point"})
    >show dbs
    local      0.78125GB
    mydb       0.23012GB
    test       0.23012GB
    

    In MongoDB default database is test. If you didn”t create any database, then collections will be stored in test database.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Drop Collection nhận dự án làm có lương

    MongoDB – Drop Collection



    In this chapter, we will see how to drop a collection using MongoDB.

    The drop() Method

    MongoDB”s db.collection.drop() is used to drop a collection from the database.

    Syntax

    Basic syntax of drop() command is as follows −

    db.COLLECTION_NAME.drop()
    

    Example

    First, check the available collections into your database mydb.

    >use mydb
    switched to db mydb
    >show collections
    mycol
    mycollection
    system.indexes
    tutorialspoint
    >
    

    Now drop the collection with the name mycollection.

    >db.mycollection.drop()
    true
    >
    

    Again check the list of collections into database.

    >show collections
    mycol
    system.indexes
    tutorialspoint
    >
    

    drop() method will return true, if the selected collection is dropped successfully, otherwise it will return false.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Data Modeling nhận dự án làm có lương

    MongoDB – Data Modelling



    Data in MongoDB has a flexible schema.documents in the same collection. They do not need to have the same set of fields or structure Common fields in a collection’s documents may hold different types of data.

    Data Model Design

    MongoDB provides two types of data models: — Embedded data model and Normalized data model. Based on the requirement, you can use either of the models while preparing your document.

    Embedded Data Model

    In this model, you can have (embed) all the related data in a single document, it is also known as de-normalized data model.

    For example, assume we are getting the details of employees in three different documents namely, Personal_details, Contact and, Address, you can embed all the three documents in a single one as shown below −

    {
    	_id: ,
    	Emp_ID: "10025AE336"
    	Personal_details:{
    		First_Name: "Radhika",
    		Last_Name: "Sharma",
    		Date_Of_Birth: "1995-09-26"
    	},
    	Contact: {
    		e-mail: "radhika_sharma.123@gmail.com",
    		phone: "9848022338"
    	},
    	Address: {
    		city: "Hyderabad",
    		Area: "Madapur",
    		State: "Telangana"
    	}
    }
    

    Normalized Data Model

    In this model, you can refer the sub documents in the original document, using references. For example, you can re-write the above document in the normalized model as:

    Employee:

    {
    	_id: <ObjectId101>,
    	Emp_ID: "10025AE336"
    }
    

    Personal_details:

    {
    	_id: <ObjectId102>,
    	empDocID: " ObjectId101",
    	First_Name: "Radhika",
    	Last_Name: "Sharma",
    	Date_Of_Birth: "1995-09-26"
    }
    

    Contact:

    {
    	_id: <ObjectId103>,
    	empDocID: " ObjectId101",
    	e-mail: "radhika_sharma.123@gmail.com",
    	phone: "9848022338"
    }
    

    Address:

    {
    	_id: <ObjectId104>,
    	empDocID: " ObjectId101",
    	city: "Hyderabad",
    	Area: "Madapur",
    	State: "Telangana"
    }
    

    Considerations while designing Schema in MongoDB

    • Design your schema according to user requirements.

    • Combine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins).

    • Duplicate the data (but limited) because disk space is cheap as compare to compute time.

    • Do joins while write, not on read.

    • Optimize your schema for most frequent use cases.

    • Do complex aggregation in the schema.

    Example

    Suppose a client needs a database design for his blog/website and see the differences between RDBMS and MongoDB schema design. Website has the following requirements.

    • Every post has the unique title, description and url.

    • Every post can have one or more tags.

    • Every post has the name of its publisher and total number of likes.

    • Every post has comments given by users along with their name, message, data-time and likes.

    • On each post, there can be zero or more comments.

    In RDBMS schema, design for above requirements will have minimum three tables.

    RDBMS Schema Design

    While in MongoDB schema, design will have one collection post and the following structure −

    {
       _id: POST_ID
       title: TITLE_OF_POST,
       description: POST_DESCRIPTION,
       by: POST_BY,
       url: URL_OF_POST,
       tags: [TAG1, TAG2, TAG3],
       likes: TOTAL_LIKES,
       comments: [
          {
             user:''COMMENT_BY'',
             message: TEXT,
             dateCreated: DATE_TIME,
             like: LIKES
          },
          {
             user:''COMMENT_BY'',
             message: TEXT,
             dateCreated: DATE_TIME,
             like: LIKES
          }
       ]
    }
    

    So while showing the data, in RDBMS you need to join three tables and in MongoDB, data will be shown from one collection only.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Environment nhận dự án làm có lương

    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 .

    Mongodb Cloud

    Enter the required details, select the Server tab, in it you can choose the version of MongoDB, operating system and, packaging as:

    Mongodb Community

    Now install the downloaded file, by default, it will be installed in the folder C:Program Files.

    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
    

    Then you need to specify set the dbpath to the created directory in mongod.exe. For the same, issue the following commands.

    In the command prompt, navigate to the bin directory current in the MongoDB installation folder. Suppose my installation folder is C:Program FilesMongoDB

    C:UsersXYZ>d:cd C:Program FilesMongoDBServer4.2bin
    C:Program FilesMongoDBServer4.2bin>mongod.exe --dbpath "C:data"
    

    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.

    C:Program FilesMongoDBServer4.2bin>mongo.exe
    MongoDB shell version v4.2.1
    connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
    Implicit session: session { "id" : UUID("4260beda-f662-4cbe-9bc7-5c1f2242663c") }
    MongoDB server version: 4.2.1
    >
    

    This will show that MongoDB is installed and run successfully. Next time when you run MongoDB, you need to issue only commands.

    C:Program FilesMongoDBServer4.2bin>mongod.exe --dbpath "C:data"
    C:Program FilesMongoDBServer4.2bin>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 = 4.2
    

    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 run the following command.

    mongo
    

    This will connect you to running MongoDB instance.

    MongoDB Help

    To get a list of commands, type db.help() in MongoDB client. This will give you a list of commands as shown in the following screenshot.

    DB Help

    MongoDB Statistics

    To get stats about MongoDB server, type the command db.stats() in MongoDB client. This will show the database name, number of collection and documents in the database. Output of the command is shown in the following screenshot.

    DB Stats

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí MongoDB – Overview nhận dự án làm có lương

    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.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc