Hướng dẫn mongodbdrivermanager

Cá nhân tôi đã gặp phải trường hợp không có liên kết đến 'nhà cung cấp / autoload.php' . Nó bắt đầu hoạt động sau khi mã của tôi trông giống như sau:

  $DB_CONNECTION_STRING="mongodb://YourCredentials";
  require '../vendor/autoload.php';
  $manager = new MongoDB\Driver\Manager[ $DB_CONNECTION_STRING ];

Sau đó, nếu bạn sử dụng MongoDB \ Driver \ Manager, một phiên bản hiện đại của trình điều khiển MongoDB, bạn thực hiện các hoạt động CRUD như sau:

Tạo một tài liệu trong bộ sưu tập :

$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc = ['name' => 'John', age => 33, profession => 'Guess what?'];
$bulkWrite->insert[$doc];
$manager->executeBulkWrite['db.MyCollection', $bulkWrite];

Đọc tài liệu trong bộ sưu tập theo tên với giới hạn:

$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query[$filter, $options];
$manager->executeQuery['db.MyCollection', $query];

Đọc tài liệu trong bộ sưu tập của MongoDb _id với giới hạn:

$filter = ['_id' => new MongoDB\BSON\ObjectID[ '5bdf54e6d722dc000f0aa6c2' ]];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query[$filter, $options];
$manager->executeQuery['db.MyCollection', $query];    

Cập nhật tài liệu trong bộ sưu tập : [Đọc thêm về các tùy chọn upsert và multi tại đây ]

$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John'];
$update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
$options = ['multi' => false, 'upsert' => false];
$bulkWrite->update[$filter, $update, $options];
$manager->executeBulkWrite['db.MyCollection', $bulkWrite];    

Xóa tài liệu trong bộ sưu tập - Xóa :

$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John', age => 35];
$options = ['limit' => 1];
$bulkWrite->delete[$filter, $options];
$manager->executeBulkWrite['db.MyCollection', $bulkWrite];

1 hữu ích 0 bình luận chia sẻ

Docs HomeMongoDB Manual

CRUD operations create, read, update, and delete documents.

Create or insert operations add new documents to a collection. If the collection does not currently exist, insert operations will create the collection.

MongoDB provides the following methods to insert documents into a collection:

  • db.collection.insertOne[]New in version 3.2

  • db.collection.insertMany[]New in version 3.2

In MongoDB, insert operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

For examples, see Insert Documents.

Read operations retrieve documents from a collection; i.e. query a collection for documents. MongoDB provides the following methods to read documents from a collection:

  • db.collection.find[]

You can specify query filters or criteria that identify the documents to return.

For examples, see:

  • Query Documents

  • Query on Embedded/Nested Documents

  • Query an Array

  • Query an Array of Embedded Documents

Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection:

  • db.collection.updateOne[]New in version 3.2

  • db.collection.updateMany[]New in version 3.2

  • db.collection.replaceOne[]New in version 3.2

In MongoDB, update operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

You can specify criteria, or filters, that identify the documents to update. These filters use the same syntax as read operations.

For examples, see Update Documents.

Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:

  • db.collection.deleteOne[]New in version 3.2

  • db.collection.deleteMany[]New in version 3.2

In MongoDB, delete operations target a single collection. All write operations in MongoDB are atomic on the level of a single document.

You can specify criteria, or filters, that identify the documents to remove. These filters use the same syntax as read operations.

For examples, see Delete Documents.

MongoDB provides the ability to perform write operations in bulk. For details, see Bulk Write Operations.

Chủ Đề