如何强制PHP Mongo驱动程序将结果作为对象而不是数组返回

问题描述:

I am using PHP Mongo extension to connect to Mongo DB server. The problem is that even though I insert the values into Mongo DB as objects, I am always getting the results in Array format when I use find or findOne in PHP. I found it really ugly and hacky.

Is there a way to make PHP retain the original structure as it is in Mongo DB ?

我正在使用PHP Mongo扩展来连接Mongo DB服务器。 问题是,即使我将值作为对象插入Mongo DB,当我在PHP中使用 find code>或 findOne code>时,我总是以Array格式获得结果。 我发现它真的很丑陋和黑客。 p>

有没有办法让PHP保留Mongo DB中的原始结构? p> div>

This is the same problem as raw JSON has in PHP, which is why json_decode() has an option to get returned results as array (defaulting being an object).

Currently (version 1.3.7 and 1.4.0-dev) do not have such option. We have talked about adding that option, and I just created a tracking ticket for it now, which will potentially be fixed later in the 1.4.x series.