$query = array()
[, array $fields = array()
]] ) : MongoCursor
query
Ҫ�������ֶΡ� MongoDB �IJ�ѯ����ʮ�ֿ��� PHP �����ڼ������е�����»�Ѳ�ѯֱ�Ӵ���������������Ķ� MongoDB ���� » find �ĺ����ĵ��Ǹ���������⡣
��ȷ������ָ���IJ�ѯ���������� $ ��ͷ�����õ����ŵģ����� PHP �Ų��᳢���� $exists ������ֵ���滻 "$exists" ���
fields
���ؽ�����ֶΡ�Array �ĸ�ʽ�� array('fieldname' => true, 'fieldname2' => true)�� _id �ֶ��ܻ᷵�ء�
��������������αꡣ
Example #1 MongoCollection::find() ����
��������ʾ�˻���������ѡ�
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'produce');
// ����ˮ��
$fruitQuery = array('Type' => 'Fruit');
$cursor = $collection->find($fruitQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
// ������IJ�Ʒ Taste is a child of Details.
$sweetQuery = array('Details.Taste' => 'Sweet');
echo "Sweet\n";
$cursor = $collection->find($sweetQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
�������̻������
array(4) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "50a87dd084f045a19b220dd6"
}
["Name"]=>
string(5) "Apple"
["Type"]=>
string(5) "Fruit"
["Details"]=>
array(2) {
["Taste"]=>
string(5) "Sweet"
["Colour"]=>
string(3) "Red"
}
}
array(4) {
["_id"]=>
object(MongoId)#8 (1) {
["$id"]=>
string(24) "50a87de084f045a19b220dd7"
}
["Name"]=>
string(5) "Lemon"
["Type"]=>
string(5) "Fruit"
["Details"]=>
array(2) {
["Taste"]=>
string(4) "Sour"
["Colour"]=>
string(5) "Green"
}
}
Sweet:
array(4) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "50a87dd084f045a19b220dd6"
}
["Name"]=>
string(5) "Apple"
["Type"]=>
string(5) "Fruit"
["Details"]=>
array(2) {
["Taste"]=>
string(5) "Sweet"
["Colour"]=>
string(3) "Red"
}
}
��������α����ʹ�õ���Ϣ���μ� MongoCursor��
Example #2 MongoCollection::find() ����
���������ʾ���������һ����Χ��
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
// search for documents where 5 < x < 20
$rangeQuery = array('x' => array( '$gt' => 5, '$lt' => 20 ));
$cursor = $collection->find($rangeQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
�������̻������
array(2) {
["_id"]=>
object(MongoId)#10 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000000"
}
["x"]=>
int(12)
}
array(2) {
["_id"]=>
object(MongoId)#11 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000001"
}
["x"]=>
int(12)
}
��������α����ʹ�õ���Ϣ���μ� MongoCursor��
Example #3 ʹ�� $where �� MongoCollection::find() ����
���������ʾ���������һ�����ϣ����� javascript ������ɸѡ�������
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$js = "function() {
return this.name == 'Joe' || this.age == 50;
}";
$cursor = $collection->find(array('$where' => $js));
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
�������̻������
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
Example #4 ʹ�� $in �� MongoCollection::find() ����
���������ʾ��ʹ�� $in ���������������ϡ�
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$cursor = $collection->find(array(
'name' => array('$in' => array('Joe', 'Wendy'))
));
?>
�������̻������
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
Example #5 ��������ʽ��ȡ�����
���� MongoCursor�� �����ڿ�ʼ��ʱ�����Ǹ�ϰ��ʹ�����顣 ʹ�� iterator_to_array() ���α�ת����һ�����顣
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
$cursor = $collection->find();
$array = iterator_to_array($cursor);
?>
�������̻������
array(3) {
["4ebc40af10b89f5149000000"]=>
array(2) {
["_id"]=>
object(MongoId)#6 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000000"
}
["x"]=>
int(12)
}
["4ebc40af10b89f5149000001"]=>
array(2) {
["_id"]=>
object(MongoId)#11 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000001"
}
["x"]=>
int(12)
}
["4ebc40af10b89f5149000002"]=>
array(3) {
["_id"]=>
object(MongoId)#12 (1) {
["$id"]=>
string(24) "4ebc40af10b89f5149000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
}
ʹ�� iterator_to_array() ����������ǿ����������������������ڴ棬���ԶԳ����ڴ��С�Ľ������Ҫ��ô����
ͬʱ����Щϵͳ���ϲ����� _id �ֶΡ�
����㴦��һ������û�� _id �ֶεļ��ϣ���Ҫ�� FALSE ����
iterator_to_array() �ڶ������������������᳢��ʹ�ò����ڵ� _id ��ֵ��Ϊ���������