Home >Database >MongoDB >Creation and deletion of MongoDB documents (php code example)

Creation and deletion of MongoDB documents (php code example)

齐天大圣
齐天大圣Original
2020-08-21 12:03:042501browse

Note that all code examples in this article are demonstrated using PHP code.

##Install the MongoDB extension

# #Extension package installation address: https://pecl.php.net/package/mongodb

Download the most stable version, and then upload the expansion package to the server.

# 解压
tar zxf mongodb-1.8.0.tgz 
cd mongodb-1.8.0

# 安装
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config 
make & make install

# 修改php配置文件php.ini
# 加入一行extension=mongodb

# 测试
php -m | grep mongodb

mongodb extension tutorial: https://docs.mongodb.com/php-library/current/reference/

Using phplib

composer require mongodb/mongodb

Insert documentSimilar to Mysql, MongoDB can also insert single or multiple documents into documents. Let’s look at inserting a single entry:

$mongo = new MongoDB\Client();
$collect = $mongo->users->users;
$collect->insertOne(['name'=> 'james', 'age' => 35]);

If the _id field is not declared, this operation will automatically create an _id field for the new document. Of course, we can also manually specify the value of _id

$collect->insertOne(['_id' => 1,'name'=> 'james', 'age' => 35]);

This function returns the MongoDB\InsertOneResult object if successfully executed, and an exception will be thrown if it fails.

Let’s see how to insert multiple documents:

$collect->insertMany([
    [ 'name'=>'paul', 'age' => 34],
    [ 'name'=>'durant', 'age' => 31],
    [ 'name'=> 'curry', 'age' => 31]
]);

It should be noted that during batch insertion, if one of the documents fails to be inserted, the subsequent ones will not continue to be inserted, but The previous one will be inserted.

# 第一条会插入成功,第二条时插入失败,后面的也不会继续插入
$collect->insertMany([
    [ 'name'=>'paul', 'age' => 34],
    ['_id'=> 1, 'name'=> 'jeans', 'age' => 1], // _id=1已存在
    [ 'name'=>'durant', 'age' => 31],
    [ 'name'=> 'curry', 'age' => 31]
]);

If you want to ignore errors and continue inserting, you need to add an option ordered to the method and set it to false.

$collect->insertMany([
    [ 'name'=>'jay', 'age' => 34],
    ['_id'=> 1, 'name'=> 'jeans', 'age' => 1], // _id=1已存在
    [ 'name'=>'xtf', 'age' => 31],
],['ordered' => false]);

Note: You can insert if you encounter the above error Success, but the statement throws an exception. If you want to ignore the error and continue the execution of the program, you need to catch the exception.

Delete document

Note: Deletion is a dangerous operation and cannot be restored or undone.

Delete documents through query statements:

/**
 * 目前有4个name为james的文档
 */
 
# 删除一个文档
$ret = $collect->deleteOne(['name'=>'james']);
printf($ret->getDeletedCount());  // 1

# 删除满足条件的所有文档
$ret = $collect->deleteMany(['name'=>'james']);
printf($ret->getDeletedCount());

Delete all documents (in fact, the entire collection is deleted):

$collect->drop();

Creation and deletion of MongoDB documents, It's very simple to use.


Recommended: "

MongoDB Video Tutorial

"

The above is the detailed content of Creation and deletion of MongoDB documents (php code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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