簡體   English   中英

如何使用 PHP “mongodb” 擴展從 MongoDB 獲取原始 BSON

[英]How to get raw BSON from MongoDB with PHP “mongodb” extension

我正在嘗試使用 PHP 從 mongodb 獲取二進制 BSON 字符串,但我不知道如何將文檔作為 BSON 獲取。 它總是首先將其解析為 PHP object。

I've tried to set a typeMap on the cursor but all this does is pass the already parsed PHP object to a bsonSerialize($object) function in the class.

代碼如下所示:

<?php

$db = new \MongoDB\Driver\Manager('mongodb://localhost/test');
$query = new \MongoDB\Driver\Query([]);
$cursor = $db->executeQuery("test.contacts", $query);

foreach($cursor as $document) {
    // $document is already parsed to a PHP stdObject
}

在最近的 MongoDB 版本(自 3.6 起)中,文檔作為響應的一部分返回,整個響應被序列化為 BSON。 這意味着,驅動程序在收到響應時,必須對整個響應進行 BSON 反序列化,然后將部分響應作為找到的文檔提供給應用程序。

驅動程序沒有一刻將文檔單獨序列化為 BSON。

因此,如果您需要將單個文檔序列化為 BSON,則需要自己進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM