PHP extension to mongodb (a small test)_javascript skills
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
try {
$mongo = new Mongo("mongodb://username:password@127.0.0.1:27017/db1");
}catch(MongoConnectionException $e) {
print $e->getMessage() ;
exit;
}
Select database blog
$db = $mongo->blog;
Close database
$conn->close();
Select operation collection
$collection = $db-> ;users;
Insert data
$user = array ('name' => 'caleng', 'city' => 'beijing');
$collection->insert($user);
Modify data
$newdata = array('$set' => array(" city" => "shanghai"));
$collection->update(array("name" => "caleng"), $newdata);
Delete data
$collection->remove(array('name'= >'caleng'), array("justOne" => true));
Find data
Find a piece of data
$result= $collection->findone(array("name"=>"caleng"));
Query a list
// 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
$cursor = $collection->find(array(
'name' => array('$in' => array('Joe', 'Wendy'))
));
group query
$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:
[{"category":"fruit","items":["apple","peach","banana "]},{"category":"veggie","items":["corn","broccoli"]}]
It can be seen that the result is a two-dimensional array
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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.

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

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.

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.

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.
