MongoDB study notes (1) Introduction and installation of MongoDB

This is the first of a series of MongoDB study notes. It mainly introduces what is a non-relational database MongoDB, how to download it, where to download it, and how to install it correctly. I. Introduction Recently, I started to learn the non-relational database MongoDB, but I couldn’t find a more systematic tutorial on the blog garden. Many materials have to be consulted on the English website, which is relatively inefficient. I am not talented, so I took the opportunity of self-study to record all my experience and experience, so that interested children’s shoes can be shared and discussed. Some resources come from other blogs, aiming to gather scattered knowledge points together. If your rights are violated, please contact [email protected]. Most of the content is original, welcome to reprint and share, but don’t forget to indicate the author and the original link when reprinting. 2. Introduction to MongoDB MongoDB is a high-performance, open source, schema-free document database, which is one of the more popular NoSql databases. It can be used as an alternative to traditional relational databases or key/value stores in many scenarios. Mongo is developed using C++. Mongo’s official website address is:, readers can get more detailed information…

MongoDB study notes (2) realize basic data operation through samus driver

1. About the driver of MongoDB MongoDB supports drivers in multiple languages, and we only introduce C# drivers here. There are many kinds of C# drivers, and the form of each driver is roughly the same, but the details are different, so the code cannot be used universally. The more commonly used ones are the official driver and the samus driver. In addition to supporting general forms of operations, the samus driver also supports linq to manipulate data. Everyone prefers this way. Official driver download address: Click to download Samus driver download address: Click to download This article will start with the samus driver to explain database access, international practices, and access to “Hello World!”. 2. Realize HelloWorld access through the samus driver Before performing the following operations, please make sure that the MongoDB service has been started. If you don’t know how to start the service, please read the previous article. Download the driver, create a new console project, and add a reference to MongoDB.dll. If you downloaded the driver source code, just compile and reference the generated DLL. The basic code is as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13 14…

MongoDB study notes (3) Manipulating MongoDB data through Jqgrid table in MVC mode

See the figure below, which is to realize the basic addition, deletion, query and modification of table data through Jqgrid. Table data addition, deletion, and modification are common functions in the development of general enterprise application systems, but the difference is that the source of this table data is the non-relational database MongoDB. Although the concept of nosql is novel, it is relatively easy to implement the basic application of MongoDB, and even the code is more concise than the basic access relational data source. Due to its own “non-relational” data storage method, the link of object-relational mapping is meaningless for MongoDB, so we will not introduce the so-called “ORM” framework to MongoDB. Next, we will explain step by step how to read MongoDB data in MVC mode and display it on the Jqgrid table in the foreground. The basic design idea of ​​this “simple system” is as follows: we display tables in the view layer, Jqgrid-related Js logic is all placed in a Js file, the control layer implements the four services of “addition, deletion, check and modification”, and the basic data access of MongoDB Implemented on the model layer. Let’s implement it step by step. the 1.…

MongoDB study notes (four) use MongoDB’s document structure to describe data relationships

A collection of MongoDB can be regarded as a table of a relational database, and a document object (document) can be regarded as a record of a relational database. But the two are not exactly equivalent. The structure of the table is fixed, and the MongoDB collection does not have this constraint; in addition, the document objects stored in the collection can even embed sub-documents, or “sub-collections”. They can all be described in a format similar to BJSON in the end. Let’s analyze the unique data management method brought by this feature of MongoDB today. Let’s take the samus driver as an example to analyze. The samus driver supports two ways to access the database, the basic way and the linq way. The basic way was introduced in the previous article. I don’t want to explain the application examples separately for the linq way. I will use two ways in this article. Ways to compare presentations. 1. Collection operations containing sub-documents There is such an application scenario. A website provides the function of member login. Users need to register an account to enjoy membership services. However, registrants may give up filling in the user information form because the input items…

MongoDB study notes (5) MongoDB file access operation

Because the document structure of MongoDB is BJSON format (BJSON full name: Binary JSON), and BJSON format itself supports saving data in binary format, so you can put The data in the binary format of the file is saved directly into the MongoDB document structure. However, since the maximum length of a BJSON cannot exceed 4M, the maximum file that can be stored in a single document cannot exceed 4M. In order to provide support for large-capacity file access, the samus driver provides “GridFS” mode to support, and “GridFS” mode file operations need to introduce a new assembly “MongoDB.GridFS.dll”. Below we implement it in two ways.   1. Access files in the document object When the file size is small, it is simpler to implement directly into the document object. For example, the access of a large number of picture files, etc., the general picture files will not exceed 4M. Let’s implement an example of uploading pictures and storing them in the database, and then taking them out and writing them back to the page: 1. Save the picture in BJSON /// /// Save the picture in BJSON /// public void SaveImgBJSON(byte[] byteImg) { Document doc = new Document(); doc[“ID”]…

MongoDB study notes (6) MongoDB index usage and efficiency analysis

1. Create an index 1. Default index MongoDB has a default “_id” key, which is equivalent to the role of “primary key”. After the collection is created, the system will automatically create an index on the “_id” key, which is the default index, and the index name is “_id_”, which cannot be deleted. We can check it with: var _idIndex = mongoCollection.Metadata.Indexes.Single(x => x.Key == “_id_”); Console.WriteLine(_idIndex); 2. Single column index The index created on a single key is a single-column index. For example, we want to create a single-column index for the “UserName” key on the “UserInfo” collection. The syntax is as follows: (1 indicates positive order, -1 reverse order) mongoCollection.Metadata.CreateIndex(new Document { { “UserName”, 1 } }, false); Next, we use the same method to find the index named “_UserName_” var _UserName_Index = mongoCollection.Metadata.Indexes.Single(x => x.Key == “_UserName_”); Console.WriteLine(_UserName_Index); 3. Composite index In addition, we can also create composite indexes for multiple keys at the same time. The following code creates a combined index according to the positive order of “UserId” and the reverse order of “UserName”: mongoCollection.Metadata.CreateIndex(new Document { { “UserId”, 1 }, { “UserName”, -1 } }, false); 4. Sub-document index We can create various indexes…

Detailed usage process of MongoDB in PHP

$mongodb = new Mongo(); //$cOnnection= new Mongo( “$dburl:$port” ); // connect to a remote host (default port) $mydb = $mongodb->mydb; // implicitly create database mydb $mydb = $mongodb->selectDB(“mydb”); //Directly select the existing database $collection = $mydb->mycollect; //Select the collection used, if it does not exist, it will be created automatically $collection = $db->selectCollection(‘mydb’); // only select, not create //insert a new record $collection->insert(array(“name”=>”l4yn3”, “age”=>”10”, “sex”:”unknown”)); the //Modify record $where = array(“name”=>”l4yn3”); $update_item = array(‘$set’=>array(“age”=>”15”, “sex”:”secret”)); $collection->update($where, $update_item); $options[‘multiple’] = true; //The default is false, whether to change the matching multiple lines $collection->update($where, $update_item, $options); the //Query the records $myinfo= $collection->findOne(array(“name”=>”l4yn3”)); $myinfo = $collection->findOne(array(“name”=> “l4yn3”), array(“age”=>”15”)); the //Search by condition: $query = array(“name”=>”l4yn3”); $cursor = $collection->find($query); //Find documents satisfying $query in the $collectio collection while($cursor->hasNext()) { var_dump($cursor->getNext()); // returns the array } the //Return the number of document records $collection->count(); the //Delete a database: $connection->dropDB(“…”); //List all available databases: $m->listDBs(); //no return value //Close the connection: $connection->close(); Various parameter methods for connecting to mongodb database in php //Connect to localhost:27017 $cOnn= new Mongo(); //Connect to the default port of the remote host $cOnn= new Mongo(‘’); //Connect to port 22011 of the remote host $cOnn= new Mongo(‘’); //MongoDB has username and password…

The configuration process of the combination of MongoDB and PHP under the window system

By learning how to use and deploy MongoDB, I tried how to apply mongodb to php programs. 1. Preparatory work First of all, mongodb must be prepared, deployed according to relevant methods, and the service can run normally. For beginners, you can refer to the tutorial of “Code Farmer”, which is very detailed and practical. the d:\mongodb\bin>net start MongoDB Preparation of php and apache environment If you are lazy, you can go to XAMPP to download an integrated installation package, including php/apache/mysql, etc., and the environment is already integrated. You only need to put the php program under htdocs/ to access it. XAMPP address: (complete installation and application) the The drivers of mongodb are some dynamic link library files. the Address: the 2. The integration of mongodb and php (1). Find the ext directory in xampp: For example, mine is installed on the D drive: D:\xampp\php\ext (2). Copy the downloaded dynamic link library file to the above directory D:\xampp\php\ext “ php_mongo-1.4.5-5.5-vc11.dll copy to D:\xampp\php\ext (3). Modify the configuration of php.ini Location: D:\xampp\php.ini   Open this file with an editor and find ; Windows Extensions “ And add a line of configuration at the end of this block (approximately:…

Connection processing between MongoDB database and PHP device

Connection processing between MongoDB database and PHP device

Version 1.3 of PHP MongoDB The driver has rewritten the connection processing library. Compared with the previous version, there have been major changes in the persistent connection and connection pool. Connection management in version 1.2 Version 1.2 of the driver introduces a connection pool. When executing any query, a connection will be requested from the connection pool, and then returned to the connection pool after completion. Completion here means that the variable holding the connection leaves its scope, here is an example. The simplest version: demo->test; $c->insert( array( ‘test’ => ‘yes’ ) ); ?> ← $m leaves the scope and the connection is returned to the connection pool In the function: demo->test; $c->insert( array( ‘test’ => ‘ yes’ ) ); } // ← $m leaves the scope, the connection is returned to the connection pool ?> In some cases, the system may generate a large number of connections, such as referencing connection objects in a complex structure of ORMs/ODMs, as in the following example: <?php for ($i = ; $i Connection management in version 1.3 In version 1.3, connection management has been greatly changed. Each worker process (thread, PHP-FPM or Apache worker), the driver separates the connection management from the…

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top