使用 php 驱动程序如何 return 计算符合给定条件的文档?
How return count of documents that match a given criteria using the php driver?
我的 collection 中的文档如下所示:
{
"state": "Wyoming",
"high": {
"fahrenheit": 115,
"city": "Basin"
},
"low": {
"fahrenheit": -63,
"city": "Moran"
}
}
显示高华氏温度大于等于120,低华氏温度小于等于-60的文档总数。
与 shell 一样,PHP 驱动程序提供 .count
method which is exactly what you need here, also in order to access the "fahrenheit" field in your embedded document to you need to use the dot notation .
$collection->count(
array(
"high.fahrenheit" => array("$gte" => 120),
"low.fahrenheit" => array("$lte" => -60)
)
)
这相当于 shell 中的以下查询:
db.collection.count({
"high.fahrenheit": { "$gte": 120 },
"low.fahrenheit": { "$lte": -60 }
})
我的 collection 中的文档如下所示:
{
"state": "Wyoming",
"high": {
"fahrenheit": 115,
"city": "Basin"
},
"low": {
"fahrenheit": -63,
"city": "Moran"
}
}
显示高华氏温度大于等于120,低华氏温度小于等于-60的文档总数。
与 shell 一样,PHP 驱动程序提供 .count
method which is exactly what you need here, also in order to access the "fahrenheit" field in your embedded document to you need to use the dot notation .
$collection->count(
array(
"high.fahrenheit" => array("$gte" => 120),
"low.fahrenheit" => array("$lte" => -60)
)
)
这相当于 shell 中的以下查询:
db.collection.count({
"high.fahrenheit": { "$gte": 120 },
"low.fahrenheit": { "$lte": -60 }
})