|
MongoCollection::findQueries this collection, returning a MongoCursor for the result set Beschreibung
public MongoCursor MongoCollection::find
([ array
$query = array()
[, array $fields = array()
]] )Parameter-Liste
RückgabewerteReturns a cursor for the search results. BeispieleBeispiel #1 MongoCollection::find example This example demonstrates basic search options.
<?phpDas oben gezeigte Beispiel erzeugt folgende Ausgabe:
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"
}
}
See MongoCursor for more information how to work with cursors. Beispiel #2 MongoCollection::find example This example demonstrates how to search for a range.
<?phpDas oben gezeigte Beispiel erzeugt folgende Ausgabe:
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)
}
See MongoCursor for more information how to work with cursors. Beispiel #3 MongoCollection::find example using $where This example demonstrates how to search a collection using javascript code to reduce the resultset.
<?phpDas oben gezeigte Beispiel erzeugt folgende Ausgabe:
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
Beispiel #4 MongoCollection::find example using $in This example demonstrates how to search a collection using the $in operator.
<?phpDas oben gezeigte Beispiel erzeugt folgende Ausgabe:
array(3) {
["_id"]=>
object(MongoId)#7 (1) {
["$id"]=>
string(24) "4ebc3e3710b89f2349000002"
}
["name"]=>
string(3) "Joe"
["age"]=>
int(20)
}
Beispiel #5 Getting results as an array This returns a MongoCursor. Often, when people are starting out, they are more comfortable using an array. To turn a cursor into an array, use the iterator_to_array function.
<?phpDas oben gezeigte Beispiel erzeugt folgende Ausgabe:
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)
}
}
Using iterator_to_array forces the driver to load all of the results into memory, so do not do this for result sets that are larger than memory!
Also, certain system collections do not have an _id
field. If you are dealing with a collection that might have documents
without _ids, pass Siehe auch
|