mongodb 工具包 CRUD
2021-05-06 本文已影响0人
Super淳语
安装mongodb 工具包
composer require mongodb/mongodb
//TODO 插入单条数据
$collection = (new MongoDB\Client)->test->users;
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin@example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
//TODO 插入多条数据
$collection = (new MongoDB\Client)->test->users;
$insertManyResult = $collection->insertMany([
[
'username' => 'admin',
'email' => 'admin@example.com',
'name' => 'Admin User',
],
[
'username' => 'test',
'email' => 'test@example.com',
'name' => 'Test User',
],
]);
printf("Inserted %d document(s)\n", $insertManyResult->getInsertedCount());
var_dump($insertManyResult->getInsertedIds());
//TODO 删除单条数据
$collection = (new MongoDB\Client)->test->users;
$collection->drop();
$collection->insertOne(['name' => 'Bob', 'state' => 'ny']);
$collection->insertOne(['name' => 'Alice', 'state' => 'ny']);
$deleteResult = $collection->deleteOne(['state' => 'ny']);
printf("Deleted %d document(s)\n", $deleteResult->getDeletedCount());
//TODO 删除多条数据
$collection = (new MongoDB\Client)->test->users;
$collection->drop();
$collection->insertOne(['name' => 'Bob', 'state' => 'ny']);
$collection->insertOne(['name' => 'Alice', 'state' => 'ny']);
$deleteResult = $collection->deleteMany(['state' => 'ny']);
printf("Deleted %d document(s)\n", $deleteResult->getDeletedCount());
// TODO 更新单条数据
$collection = (new MongoDB \ Client)->Test->User;
$collection->drop();
$collection->insertOne([['name' => 'Bob', 'state' => 'ny']]);
$collection->insertOne([['name' => 'Alice', 'state' => 'ny']]);
$updateResult = $collection->updateOne(
['state' => 'ny'],
['$set' => ['country' => 'us']]
);
printf("Matched %d files\n", $updateResult->getMatchedCount());
printf("%d documents modified\n", $updateResult->getModifiedCount());
//TODO 更新多条数据
$collection = (new MongoDB\Client)->test->users;
$collection->drop();
$collection->insertOne(['name' => 'Bob', 'state' => 'ny', 'country' => 'us']);
$collection->insertOne(['name' => 'Alice', 'state' => 'ny']);
$collection->insertOne(['name' => 'Sam', 'state' => 'ny']);
$updateResult = $collection->updateMany(
['state' => 'ny'],
['$set' => ['country' => 'us']]
);
printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
printf("Modified %d document(s)\n", $updateResult->getModifiedCount());
//TODO 查询单条数据
$collection = (new MongoDB\Client)->test->zips;
$document = $collection->findOne(['_id' => '94301']);
var_dump($document);
//TODO 查询多条数据
$collection = (new MongoDB\Client)->test->zips;
$cursor = $collection->find(['city' => 'JERSEY CITY', 'state' => 'NJ']);
foreach ($cursor as $document) {
echo $document['_id'], "\n";
}
//TODO 查询投射数据
$collection = (new MongoDB\Client)->test->restaurants;
$cursor = $collection->find(
[
'cuisine' => 'Italian',
'borough' => 'Manhattan',
],
[
'projection' => [
'name' => 1,
'borough' => 1,
'cuisine' => 1,
],
'limit' => 4,
]
);
foreach($cursor as $restaurant) {
var_dump($restaurant);
};
//TODO 查询分页排序数据
$collection = (new MongoDB\Client)->test->zips;
$cursor = $collection->find(
[],
[
'limit' => 5,
'sort' => ['pop' => -1],
]
);
foreach ($cursor as $document) {
printf("%s: %s, %s\n", $document['_id'], $document['city'], $document['state']);
}
//TODO 查询常用表达式数据
$collection = (new MongoDB\Client)->test->zips;
$cursor = $collection->find([
'city' => new MongoDB\BSON\Regex('^garden', 'i'),
'state' => 'TX',
]);
foreach ($cursor as $document) {
printf("%s: %s, %s\n", $document['_id'], $document['city'], $document['state']);
}
//TODO 聚合复杂查询
$collection = (new MongoDB\Client)->test->zips;
$cursor = $collection->aggregate([
['$group' => ['_id' => '$state', 'count' => ['$sum' => 1]]],
['$sort' => ['count' => -1]],
['$limit' => 5],
]);
foreach ($cursor as $state) {
printf("%s has %d zip codes\n", $state['_id'], $state['count']);
}