Category: mongodb

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

    MongoDB – Advanced Indexing



    we have inserted the following document in the collection named users as shown below −

    db.users.insert(
    	{
    		"address": {
    			"city": "Los Angeles",
    			"state": "California",
    			"pincode": "123"
    		},
    		"tags": [
    			"music",
    			"cricket",
    			"blogs"
    		],
    		"name": "Tom Benzamin"
    	}
    )
    

    The above document contains an address sub-document and a tags array.

    Indexing Array Fields

    Suppose we want to search user documents based on the user’s tags. For this, we will create an index on tags array in the collection.

    Creating an index on array in turn creates separate index entries for each of its fields. So in our case when we create an index on tags array, separate indexes will be created for its values music, cricket and blogs.

    To create an index on tags array, use the following code −

    >db.users.createIndex({"tags":1})
    {
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
    }
    >
    

    After creating the index, we can search on the tags field of the collection like this −

    > db.users.find({tags:"cricket"}).pretty()
    {
    	"_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
    	"address" : {
    		"city" : "Los Angeles",
    		"state" : "California",
    		"pincode" : "123"
    	},
    	"tags" : [
    		"music",
    		"cricket",
    		"blogs"
    	],
    	"name" : "Tom Benzamin"
    }
    >
    

    To verify that proper indexing is used, use the following explain command −

    >db.users.find({tags:"cricket"}).explain()
    

    This gives you the following result −

    {
    	"queryPlanner" : {
    		"plannerVersion" : 1,
    		"namespace" : "mydb.users",
    		"indexFilterSet" : false,
    		"parsedQuery" : {
    			"tags" : {
    				"$eq" : "cricket"
    			}
    		},
    		"queryHash" : "9D3B61A7",
    		"planCacheKey" : "04C9997B",
    		"winningPlan" : {
    			"stage" : "FETCH",
    			"inputStage" : {
    				"stage" : "IXSCAN",
    				"keyPattern" : {
    					"tags" : 1
    				},
    				"indexName" : "tags_1",
    				"isMultiKey" : false,
    				"multiKeyPaths" : {
    					"tags" : [ ]
    				},
    				"isUnique" : false,
    				"isSparse" : false,
    				"isPartial" : false,
    				"indexVersion" : 2,
    				"direction" : "forward",
    				"indexBounds" : {
    					"tags" : [
    						"["cricket", "cricket"]"
    					]
    				}
    			}
    		},
    		"rejectedPlans" : [ ]
    	},
    	"serverInfo" : {
    		"host" : "Krishna",
    		"port" : 27017,
    		"version" : "4.2.1",
    		"gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
    	},
    	"ok" : 1
    }
    >
    

    The above command resulted in “cursor” : “BtreeCursor tags_1” which confirms that proper indexing is used.

    Indexing Sub-Document Fields

    Suppose that we want to search documents based on city, state and pincode fields. Since all these fields are part of address sub-document field, we will create an index on all the fields of the sub-document.

    For creating an index on all the three fields of the sub-document, use the following code −

    >db.users.createIndex({"address.city":1,"address.state":1,"address.pincode":1})
    {
    	"numIndexesBefore" : 4,
    	"numIndexesAfter" : 4,
    	"note" : "all indexes already exist",
    	"ok" : 1
    }
    >
    

    Once the index is created, we can search for any of the sub-document fields utilizing this index as follows −

    > db.users.find({"address.city":"Los Angeles"}).pretty()
    {
    	"_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
    	"address" : {
    		"city" : "Los Angeles",
    		"state" : "California",
    		"pincode" : "123"
    	},
    	"tags" : [
    		"music",
    		"cricket",
    		"blogs"
    	],
    	"name" : "Tom Benzamin"
    }
    

    Remember that the query expression has to follow the order of the index specified. So the index created above would support the following queries −

    >db.users.find({"address.city":"Los Angeles","address.state":"California"}).pretty()
    {
    	"_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
    	"address" : {
    		"city" : "Los Angeles",
    		"state" : "California",
    		"pincode" : "123"
    	},
    	"tags" : [
    		"music",
    		"cricket",
    		"blogs"
    	],
    	"name" : "Tom Benzamin"
    }
    >
    

    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 – ObjectId nhận dự án làm có lương

    MongoDB – ObjectId



    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
    

    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 – Indexing Limitations nhận dự án làm có lương

    MongoDB – Indexing Limitations



    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.

    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 – Regular Expression nhận dự án làm có lương

    MongoDB – Regular Expression



    Regular Expressions are frequently used in all languages to search for a pattern or word in any string. MongoDB also provides functionality of regular expression for string pattern matching using the $regex operator. MongoDB uses PCRE (Perl Compatible Regular Expression) as regular expression language.

    Unlike text search, we do not need to do any configuration or command to use regular expressions.

    Assume we have inserted a document in a database named posts as shown below −

    > db.posts.insert(
    {
       "post_text": "enjoy the mongodb articles on tutorialspoint",
       "tags": [
          "mongodb",
          "tutorialspoint"
       ]
    }
    WriteResult({ "nInserted" : 1 })
    

    Using regex Expression

    The following regex query searches for all the posts containing string tutorialspoint in it −

    > db.posts.find({post_text:{$regex:"tutorialspoint"}}).pretty()
    {
    	"_id" : ObjectId("5dd7ce28f1dd4583e7103fe0"),
    	"post_text" : "enjoy the mongodb articles on tutorialspoint",
    	"tags" : [
    		"mongodb",
    		"tutorialspoint"
    	]
    }
    {
    	"_id" : ObjectId("5dd7d111f1dd4583e7103fe2"),
    	"post_text" : "enjoy the mongodb articles on tutorialspoint",
    	"tags" : [
    		"mongodb",
    		"tutorialspoint"
    	]
    }
    >
    

    The same query can also be written as −

    >db.posts.find({post_text:/tutorialspoint/})
    

    Using regex Expression with Case Insensitive

    To make the search case insensitive, we use the $options parameter with value $i. The following command will look for strings having the word tutorialspoint, irrespective of smaller or capital case −

    >db.posts.find({post_text:{$regex:"tutorialspoint",$options:"$i"}})
    

    One of the results returned from this query is the following document which contains the word tutorialspoint in different cases −

    {
       "_id" : ObjectId("53493d37d852429c10000004"),
       "post_text" : "hey! this is my post on TutorialsPoint",
       "tags" : [ "tutorialspoint" ]
    }
     

    Using regex for Array Elements

    We can also use the concept of regex on array field. This is particularly very important when we implement the functionality of tags. So, if you want to search for all the posts having tags beginning from the word tutorial (either tutorial or tutorials or tutorialpoint or tutorialphp), you can use the following code −

    >db.posts.find({tags:{$regex:"tutorial"}})
    

    Optimizing Regular Expression Queries

    • If the document fields are indexed, the query will use make use of indexed values to match the regular expression. This makes the search very fast as compared to the regular expression scanning the whole collection.

    • If the regular expression is a prefix expression, all the matches are meant to start with a certain string characters. For e.g., if the regex expression is ^tut, then the query has to search for only those strings that begin with tut.


    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 – Map Reduce nhận dự án làm có lương

    MongoDB – Map Reduce



    As per the MongoDB documentation, Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. MongoDB uses mapReduce command for map-reduce operations. MapReduce is generally used for processing large data sets.

    MapReduce Command

    Following is the syntax of the basic mapReduce command −

    >db.collection.mapReduce(
       function() {emit(key,value);},  //map function
       function(key,values) {return reduceFunction}, {   //reduce function
          out: collection,
          query: document,
          sort: document,
          limit: number
       }
    )
    

    The map-reduce function first queries the collection, then maps the result documents to emit key-value pairs, which is then reduced based on the keys that have multiple values.

    In the above syntax −

    • map is a javascript function that maps a value with a key and emits a key-value pair

    • reduce is a javascript function that reduces or groups all the documents having the same key

    • out specifies the location of the map-reduce query result

    • query specifies the optional selection criteria for selecting documents

    • sort specifies the optional sort criteria

    • limit specifies the optional maximum number of documents to be returned

    Using MapReduce

    Consider the following document structure storing user posts. The document stores user_name of the user and the status of post.

    {
       "post_text": "tutorialspoint is an awesome website for tutorials",
       "user_name": "mark",
       "status":"active"
    }
    

    Now, we will use a mapReduce function on our posts collection to select all the active posts, group them on the basis of user_name and then count the number of posts by each user using the following code −

    >db.posts.mapReduce(
       function() { emit(this.user_id,1); },
    
       function(key, values) {return Array.sum(values)}, {
          query:{status:"active"},
          out:"post_total"
       }
    )
    

    The above mapReduce query outputs the following result −

    {
       "result" : "post_total",
       "timeMillis" : 9,
       "counts" : {
          "input" : 4,
          "emit" : 4,
          "reduce" : 2,
          "output" : 2
       },
       "ok" : 1,
    }
    

    The result shows that a total of 4 documents matched the query (status:”active”), the map function emitted 4 documents with key-value pairs and finally the reduce function grouped mapped documents having the same keys into 2.

    To see the result of this mapReduce query, use the find operator −

    >db.posts.mapReduce(
       function() { emit(this.user_id,1); },
       function(key, values) {return Array.sum(values)}, {
          query:{status:"active"},
          out:"post_total"
       }
    
    ).find()
    

    The above query gives the following result which indicates that both users tom and mark have two posts in active states −

    { "_id" : "tom", "value" : 2 }
    { "_id" : "mark", "value" : 2 }
    

    In a similar manner, MapReduce queries can be used to construct large complex aggregation queries. The use of custom Javascript functions make use of MapReduce which is very flexible and powerful.


    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 – Text Search nhận dự án làm có lương

    MongoDB – Text Search



    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 }
    

    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í Working with Rockmongo nhận dự án làm có lương

    Working with RockMongo



    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:

    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.


    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 – Capped Collections nhận dự án làm có lương

    MongoDB – Capped Collections



    Capped collections are fixed-size circular collections that follow the insertion order to support high performance for create, read, and delete operations. By circular, it means that when the fixed size allocated to the collection is exhausted, it will start deleting the oldest document in the collection without providing any explicit commands.

    Capped collections restrict updates to the documents if the update results in increased document size. Since capped collections store documents in the order of the disk storage, it ensures that the document size does not increase the size allocated on the disk. Capped collections are best for storing log information, cache data, or any other high volume data.

    Creating Capped Collection

    To create a capped collection, we use the normal createCollection command but with capped option as true and specifying the maximum size of collection in bytes.

    >db.createCollection("cappedLogCollection",{capped:true,size:10000})
    

    In addition to collection size, we can also limit the number of documents in the collection using the max parameter −

    >db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})
    

    If you want to check whether a collection is capped or not, use the following isCapped command −

    >db.cappedLogCollection.isCapped()
    

    If there is an existing collection which you are planning to convert to capped, you can do it with the following code −

    >db.runCommand({"convertToCapped":"posts",size:10000})
    

    This code would convert our existing collection posts to a capped collection.

    Querying Capped Collection

    By default, a find query on a capped collection will display results in insertion order. But if you want the documents to be retrieved in reverse order, use the sort command as shown in the following code −

    >db.cappedLogCollection.find().sort({$natural:-1})
    

    There are few other important points regarding capped collections worth knowing −

    • We cannot delete documents from a capped collection.

    • There are no default indexes present in a capped collection, not even on _id field.

    • While inserting a new document, MongoDB does not have to actually look for a place to accommodate new document on the disk. It can blindly insert the new document at the tail of the collection. This makes insert operations in capped collections very fast.

    • Similarly, while reading documents MongoDB returns the documents in the same order as present on disk. This makes the read operation very fast.


    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í Auto-Increment Sequence nhận dự án làm có lương

    MongoDB – Auto-Increment Sequence



    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" }
    

    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 – PHP nhận dự án làm có lương

    MongoDB – PHP



    To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url . Make sure to download the latest release of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory (“ext” by default) and add the following line to your php.ini file −

    extension = php_mongo.dll
    

    Make a Connection and Select a Database

    To make a connection, you need to specify the database name, if the database doesn”t exist then MongoDB creates it automatically.

    Following is the code snippet to connect to the database −

    <?php
       // connect to mongodb
       $m = new MongoClient();
    
       echo "Connection to database successfully";
       // select a database
       $db = $m->mydb;
    
       echo "Database mydb selected";
    ?>
    

    When the program is executed, it will produce the following result −

    Connection to database successfully
    Database mydb selected
    

    Create a Collection

    Following is the code snippet to create a collection −

    <?php
       // connect to mongodb
       $m = new MongoClient();
       echo "Connection to database successfully";
    
       // select a database
       $db = $m->mydb;
       echo "Database mydb selected";
       $collection = $db->createCollection("mycol");
       echo "Collection created succsessfully";
    ?>
    

    When the program is executed, it will produce the following result −

    Connection to database successfully
    Database mydb selected
    Collection created succsessfully
    

    Insert a Document

    To insert a document into MongoDB, insert() method is used.

    Following is the code snippet to insert a document −

    <?php
       // connect to mongodb
       $m = new MongoClient();
       echo "Connection to database successfully";
    
       // select a database
       $db = $m->mydb;
       echo "Database mydb selected";
       $collection = $db->mycol;
       echo "Collection selected succsessfully";
    
       $document = array(
          "title" => "MongoDB",
          "description" => "database",
          "likes" => 100,
          "url" => "http://www.tutorialspoint.com/mongodb/",
          "by" => "tutorials point"
       );
    
       $collection->insert($document);
       echo "Document inserted successfully";
    ?>
    

    When the program is executed, it will produce the following result −

    Connection to database successfully
    Database mydb selected
    Collection selected succsessfully
    Document inserted successfully
    

    Find All Documents

    To select all documents from the collection, find() method is used.

    Following is the code snippet to select all documents −

    <?php
       // connect to mongodb
       $m = new MongoClient();
       echo "Connection to database successfully";
    
       // select a database
       $db = $m->mydb;
       echo "Database mydb selected";
       $collection = $db->mycol;
       echo "Collection selected succsessfully";
       $cursor = $collection->find();
       // iterate cursor to display title of documents
    
       foreach ($cursor as $document) {
          echo $document["title"] . "n";
       }
    ?>
    

    When the program is executed, it will produce the following result −

    Connection to database successfully
    Database mydb selected
    Collection selected succsessfully {
       "title": "MongoDB"
    }
    

    Update a Document

    To update a document, you need to use the update() method.

    In the following example, we will update the title of inserted document to MongoDB Tutorial. Following is the code snippet to update a document −

    <?php
       // connect to mongodb
       $m = new MongoClient();
       echo "Connection to database successfully";
    
       // select a database
       $db = $m->mydb;
       echo "Database mydb selected";
       $collection = $db->mycol;
       echo "Collection selected succsessfully";
       // now update the document
       $collection->update(array("title"=>"MongoDB"),
          array(''$set''=>array("title"=>"MongoDB Tutorial")));
       echo "Document updated successfully";
    
       // now display the updated document
       $cursor = $collection->find();
    
       // iterate cursor to display title of documents
       echo "Updated document";
    
       foreach ($cursor as $document) {
          echo $document["title"] . "n";
       }
    ?>
    

    When the program is executed, it will produce the following result −

    Connection to database successfully
    Database mydb selected
    Collection selected succsessfully
    Document updated successfully
    Updated document {
       "title": "MongoDB Tutorial"
    }
    

    Delete a Document

    To delete a document, you need to use remove() method.

    In the following example, we will remove the documents that has the title MongoDB Tutorial. Following is the code snippet to delete a document −

    <?php
       // connect to mongodb
       $m = new MongoClient();
       echo "Connection to database successfully";
    
       // select a database
       $db = $m->mydb;
       echo "Database mydb selected";
       $collection = $db->mycol;
       echo "Collection selected succsessfully";
    
       // now remove the document
       $collection->remove(array("title"=>"MongoDB Tutorial"),false);
       echo "Documents deleted successfully";
    
       // now display the available documents
       $cursor = $collection->find();
    
       // iterate cursor to display title of documents
       echo "Updated document";
    
       foreach ($cursor as $document) {
          echo $document["title"] . "n";
       }
    ?>
    

    When the program is executed, it will produce the following result −

    Connection to database successfully
    Database mydb selected
    Collection selected successfully
    Documents deleted successfully
    

    In the above example, the second parameter is boolean type and used for justOne field of remove() method.

    Remaining MongoDB methods findOne(), save(), limit(), skip(), sort() etc. works same as explained above.


    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