[英]PHP MongoDB query for one record using MongoId Object _id
我目前正在學習MongoDB。 我看過有關查詢集合中字段的教程。 我想知道如何使用MongoId Object _id值在PHP中進行查詢。 與我的問題最接近的答案是在Perl Mongo查找對象ID處 。
另外,有沒有一種方法可以在創建新記錄時將Object _id值記錄在該記錄的另一個字段中?
謝謝。
更新:
除了我在下面選擇的答案外,同事還發現了這一點:
這應該從mongodb網站( http://blog.mongodb.org/post/26903435041/mongodb-for-the-php-mind-part-2 )提供幫助:
// This is only a string, this is NOT a MongoId
$mongoid = '4cb4ab6d7addf98506010000';
// You will not find anything by searching by string alone
$nothing = $collection->find(array('_id' => $mongoid));
echo $nothing->count(); // This should echo 0
// THIS is how you find something by MongoId
$realmongoid = new MongoId($mongoid);
// Pass the actual instance of the MongoId object to the query
$something = $collection->find(array('_id' => $realmongoid));
echo $something->count(); // This should echo 1
關於您的問題的一部分,我不知道有任何自動方式將objectid存儲在另一個字段中,但我也不知道您為什么要這樣做-您已經將其存儲在_id中,為什么要它在另一個領域?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.