I'm performing a simple request to my MongoDB in php. It looks like this:
$result = $this->myMongoClient->myCollection->find([
'param_1' => $param_1,
'param_2' => $param_2,
]);
This returns a MongoDB\Driver\Cursor object. I need the count of returned entrys from the database.
I've googeld a while and found this. But with this function dose not exists on the object returend by MongoDB (Call to undefined method MongoDB\Driver\Cursor::count())
$result->count()
Which is the commun way to count the number of results?
I have found a solution, but don't know if it is recommendable or not.
count($result->toArray());
I don't know if you are using the MongoDB PHP Library, but if you are you can just use the following
$result = $this->myMongoClient->myCollection->countDocuments($where);
https://docs.mongodb.com/php-library/master/reference/method/MongoDBCollection-countDocuments/index.html
Related
Im a bit new to the php side of parse, mainly objective-c and swift but I need to write some code that I can query a column (not the objectID one) to return the results..
The column I'm trying to query is a pointer to another class.
Here is the very basic code I have which returns all the rows in the class and the pointers data with the include key, but I need to filter or get only the row/s that I'm looking for.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$results = $query->find();
In the php sdk I see an option to use equalTo which has a key and a value to it so I tried the following code.
so I choose the column that was the pointer , and its objectid to hopefully only return those row/s that has that object id.
$query = new ParseQuery("ClassB");
$query->includeKey("ClassA");
$query->equalTo("ColumnNameX", "yjdyaGRWP7");
$results = $query->find();
Nothing was returned and a php error was spit out
'pointer field ColumnNameX needs a pointer value' in /var/www/parse/src/Parse/ParseClient.php:326
So im not 100% sure why I cant filter by a ColumnNameX using its objectID which is a pointer to ClassA..
Did I miss something in the PHP docs..
I mean ideally in mysql to just get that row I want would be
SELECT * FROM ClassB WHERE ColunNameX = yjdyaGRWP7
That would return me the row of data, I can use a Join of course to get some info from ClassA as well.
Any thoughts on what im missing or do I need to first query the Class A to get a pointer, then in the equalTo do something like ("ColumnNamX" , $pointerfromClassA) ?
any one have anyone point out what im missing or have a code example.. I have seen some that use the objectID but I dont have access to that.
Ok I figured out one way to do this, not sure if this is the right way but it returns now what I want..
$query->equalTo("ColunNameX", array("__type" => "Pointer", "className" => "ColunNameX", "objectId" => "yjdyaGRWP7"));
i need to find the most recent element, the field "data_caricamento" save the date.
so i made a index with MognoDb shell:
db.collection.ensureIndex({"data_caricamento": -1})
and with the php code below i have what i need
$cursor=$collection->find();
$cursor->sort(array("data_caricamento"=> -1));
$cursor->limit($n);
but i think that should be a better way to do it,
for example there is a way to query the directly the index?
thx.
there is a way to query the directly the index?
Sort of. You can do a covered query here by doing:
$cursor = $collection
->find(array(), array('_id' => 0, 'data_caricamento' => 1))
->sort(array("data_caricamento" => -1))
->limit($n);
That will query only the index.
Try using the hint. And if you want to check whether the indexes got used by your query use explain().
....
$collection->find()->sort(array('data_caricamento'=>1))->hint(array('data_caricamento'=>1));
print_r($cursor->explain());
This will help you see that your queries are hitting index for faster search and sorting!
Cheers!
I'm having some difficulties to select the last entry I posted into my Mongo Collection. This is an example of what an object in my collection looks like:
{"category":1,"date:
{"sec":1356521350,"usec":0},"content":"Test Content","_id":
{"$id":"50dadf8639f992c83f000003"}}
Now, I want to sort on the field date and I am trying to do so by using the following functionality (by using the Yii-MongoDB-Suite):
$oCriteria = new EMongoCriteria;
$oCriteria->sort('date', EMongoCriteria::SORT_DESC);
$oOjbect = ObjectModel::model()->find($oCriteria);
Now, instead of returning the object which has the lastest date, it returns me the first object I entered in the collection.
I literally have no clue about what might be going wrong. Any clues?
I believe you need to pass a PHP array to sort(), so what you actually need is:
$oCriteria = new EMongoCriteria;
$oCriteria->sort(array('date', EMongoCriteria::SORT_DESC));
$oOjbect = ObjectModel::model()->find($oCriteria);
New to Mongo here. I'm using the PHP lib and trying to work out how I can find in a collection from multiple queries. I could do this by repeating the query with a different query, but I wondered if it can be done in one.
I.e.
$idsToLookFor = array(2124,4241,5553);
$query = $db->thisCollection->find(array('id' => $idsToLookFor));
That's what I'd like to do. However it doesn't work. What I'm trying to do is find a set of results for all the id's at one time.
Possible or just do a findOne on each with a foreach/for?
Use $in Operator
$idsToLookFor = array(2124,4241,5553);
$query = $db->thisCollection->find(array(
'id' => array('$in' => $idsToLookFor)
));
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$query = $dm->createQueryBuilder('MyBundle:Listing')
->select('title')
->field('coordinates')->geoNear(
(float)$longitude,
(float)$latitude
)->spherical(true);
$classifieds_array = $classifieds->toArray();
$data = array('success'=>true,'classifieds' => $classifieds_array,
'displaymessage' => $classifieds->count(). " Search Results Found");
Even though I am selecting just one field, for my result set, I am getting every thing back in collection along with title. Is this a bug?
NOTE: I commented out the ->field('coordinates')->geoNear((float)$longitude, (float)$latitude)->spherical(true) line and now the select seems to work. This is crazy.
The geoNear command in MongoDB doesn't seem to support filtering result fields, according to the documentation examples. Only a query option is supported to limit matched documents.
In your case, it also looks like mixing up the geoNear() and near() builder methods in Doctrine. Since you're operating on the coordinates field, the appropriate syntax would be near(). geoNear() is a top-level method to tell the builder you wish to use the command, which doesn't require a field name since it uses the one and only geospatial index on the collection.
For usage examples, I would advise looking at the query and builder unit tests in the Doctrine MongoDB library.