使锟斤拷 MongoCollection::findOne() 锟斤拷锟斤拷

要锟介看锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟诫到锟斤拷锟捷匡拷锟斤拷牡锟斤拷锟斤拷锟斤拷约虻サ锟绞癸拷锟� MongoCollection::findOne() 锟斤拷锟斤拷锟接硷拷锟斤拷锟斤拷锟斤拷一锟斤拷锟津单碉拷锟侥碉拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟街伙拷锟斤拷询一锟斤拷锟斤拷锟斤拷锟绞憋拷锟斤拷锟斤拷锟矫★拷

<?php
$connection 
= new MongoClient();
$collection $connection->database->collectionName;

$document $collection->findOne();
var_dump$document );
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

array(6) {
  ["_id"]=>
  object(MongoId)#8 (1) {
    ["$id"]=>
    string(24) "4e2995576803fab768000000"
  }
  ["name"]=>
  string(7) "MongoDB"
  ["type"]=>
  string(8) "database"
  ["count"]=>
  int(1)
  ["info"]=>
  array(2) {
    ["x"]=>
    int(203)
    ["y"]=>
    int(102)
  }
  ["versions"]=>
  array(3) {
    [0]=>
    string(5) "0.9.7"
    [1]=>
    string(5) "0.9.8"
    [2]=>
    string(5) "0.9.9"
  }
}

注锟解:锟斤拷一锟斤拷 _id 锟街段憋拷锟皆讹拷锟斤拷拥锟斤拷锟斤拷锟侥碉拷锟斤拷锟剿★拷 _id 锟街段撅拷锟角硷拷锟较碉拷"锟斤拷锟斤拷"锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷牡锟斤拷锟绞憋拷锟斤拷锟矫伙拷锟斤拷侄锟街革拷锟斤拷锟斤拷锟斤拷锟斤拷突锟斤拷远锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥碉拷锟斤拷锟斤拷锟斤拷 _id 锟街段o拷锟斤拷么锟斤拷锟节硷拷锟斤拷锟叫憋拷锟斤拷锟斤拷唯一锟侥★拷 锟斤拷锟斤拷一锟斤拷锟斤拷锟接o拷

<?php
$connection 
= new MongoClient();
$db $connection->database;

$db->foo->insert(array("_id" => 1));
// this will throw an exception
$db->foo->insert(array("_id" => 1));

// this is fine, as it is a different collection
$db->bar->insert(array("_id" => 1));
?>

默锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟斤拷锟斤拷锟节凤拷锟斤拷锟斤拷通锟斤拷锟斤拷写锟斤拷锟斤拷锟斤拷蠓祷兀锟斤拷锟阶拷锟�"通锟斤拷"锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷牡锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷写锟诫,锟解并锟斤拷锟斤拷味锟斤拷锟斤拷锟斤拷锟窖撅拷写锟斤拷锟斤拷蹋锟斤拷锟斤拷锟斤拷锟斤拷通锟斤拷锟斤拷锟节讹拷锟斤拷锟斤拷锟斤拷锟斤拷为 array("w" => 0) 锟斤拷锟侥憋拷默锟斤拷锟斤拷为锟斤拷锟斤拷时锟斤拷牟锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截o拷锟斤拷锟揭诧拷锟斤拷锟阶筹拷 _id 锟截革拷锟斤拷锟届常锟斤拷

锟轿硷拷

MongoCollection::findOne() 锟斤拷锟斤拷锟斤拷锟侥碉拷锟斤拷锟叫对诧拷询锟斤拷锟斤拷细说锟斤拷锟斤拷

唯一ID锟斤拷锟斤拷息锟介看 MongoId

写锟斤拷 锟斤拷锟街革拷锟斤拷细锟斤拷说锟斤拷锟斤拷写锟斤拷锟斤拷, Write Concerns 锟铰斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟揭恍┬达拷锟斤拷锟斤拷锟窖★拷睢�