Home Web Front-end JS Tutorial PHP extension to mongodb (a small test)_javascript skills

PHP extension to mongodb (a small test)_javascript skills

May 16, 2016 pm 05:48 PM
mongodb Expand

There is a strong wind blowing outside today. Being able to write a blog in a warm hut is also a kind of happiness for Beidiao. Okay, without further ado, today I will mainly talk about PHP connection and operation of mongodb. If you have not read the content of the last two issues and do not know how to install the PHP extension to mongodb, please go back and read " PHP to mongodb" Extension (first acquaintance) " and "PHP extension to mongodb (newbie) ".

php connects mongodb

Copy code The code is as follows:

try {
$mongo = new Mongo("mongodb://username:password@127.0.0.1:27017/db1");
}catch(MongoConnectionException $e) {
print $e->getMessage() ;
exit;
}

Select database blog
Copy code The code is as follows :

$db = $mongo->blog;

Close database
Copy code The code is as follows:

$conn->close();

Select operation collection
$collection = $db-> ;users;
Insert data
Copy code The code is as follows:

$user = array ('name' => 'caleng', 'city' => 'beijing');
$collection->insert($user);

Modify data
Copy code The code is as follows:

$newdata = array('$set' => array(" city" => "shanghai"));
$collection->update(array("name" => "caleng"), $newdata);

Delete data
Copy code The code is as follows:

$collection->remove(array('name'= >'caleng'), array("justOne" => true));

Find data
Find a piece of data
Copy code The code is as follows:

$result= $collection->findone(array("name"=>"caleng"));

Query a list
Copy code The code is as follows:

// Find data whose creation time is greater than a certain time
$start = 1;
$counditionarray=array("ctime"=>array('$gt'=>1337184000));
$list_data = $this->game_handle->find($counditionarray);
$total = $this->game_handle->count($counditionarray);
$list_data->limit($count) ; //Data end position
$list_data->skip($start); //Data start position
var_dump($list_data);

in query
Copy code The code is as follows:

$cursor = $collection->find(array(
'name' => array('$in' => array('Joe', 'Wendy'))
));

group query
Copy code The code is as follows:

$collection->insert(array("category" => "fruit", "name" => "apple"));
$collection->insert(array(" category" => "fruit", "name" => "peach"));
$collection->insert(array("category" => "fruit", "name" => " banana"));
$collection->insert(array("category" => "veggie", "name" => "corn"));
$collection->insert(array ("category" => "veggie", "name" => "broccoli"));
$keys = array("category" => 1);
$initial = array("items " => array());
$reduce = "function (obj, prev) { prev.items.push(obj.name); }";
$g = $collection->group( $keys, $initial, $reduce);
echo json_encode($g['retval']);

Output result:
Copy code The code is as follows:

[{"category":"fruit","items":["apple","peach","banana "]},{"category":"veggie","items":["corn","broccoli"]}]

It can be seen that the result is a two-dimensional array
Copy code The code is as follows:

array(
0 => array("category" =>"fruit", "items"=>array("apple","peach","banana")),
1 => array("category" =>"veggie", "items "=>array("corn","broccoli"))
)

Here are some simple operations written here. If you want to use php to work better with mongodb, then Read the manual.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Which version is generally used for mongodb? Which version is generally used for mongodb? Apr 07, 2024 pm 05:48 PM

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

Where is the database created by mongodb? Where is the database created by mongodb? Apr 07, 2024 pm 05:39 PM

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

How to install mbstring extension under CENTOS7? How to install mbstring extension under CENTOS7? Jan 06, 2024 pm 09:59 PM

1.UncaughtError:Calltoundefinedfunctionmb_strlen(); When the above error occurs, it means that we have not installed the mbstring extension; 2. Enter the PHP installation directory cd/temp001/php-7.1.0/ext/mbstring 3. Start phpize(/usr/local/bin /phpize or /usr/local/php7-abel001/bin/phpize) command to install php extension 4../configure--with-php-config=/usr/local/php7-abel

What are the advantages of mongodb database What are the advantages of mongodb database Apr 07, 2024 pm 05:21 PM

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

What does mongodb mean? What does mongodb mean? Apr 07, 2024 pm 05:57 PM

MongoDB is a document-oriented, distributed database system used to store and manage large amounts of structured and unstructured data. Its core concepts include document storage and distribution, and its main features include dynamic schema, indexing, aggregation, map-reduce and replication. It is widely used in content management systems, e-commerce platforms, social media websites, IoT applications, and mobile application development.

How to open mongodb How to open mongodb Apr 07, 2024 pm 06:15 PM

On Linux/macOS: Create the data directory and start the "mongod" service. On Windows: Create the data directory and start the MongoDB service from Service Manager. In Docker: Run the "docker run" command. On other platforms: Please consult the MongoDB documentation. Verification method: Run the "mongo" command to connect and view the server version.

See all articles