简体   繁体   中英

MongoDB from PHP strange behavior

We have a virtual machine containing a MongoDB installation as well as a php5 and apache 2 Server. In this virtual machine (Machine A) when we execute a range query involving a "$lt" opperator from the MongoDB shell the query executes normaly returning some results. When we execute the same query from php no result is returned. The strange is that when we execute from PHP a "$gt" query the results are correct.

We changed the MongoDB host to a different machine lets say B. On the machine B we execute the php script with the "$lt" operator and runs correctly. The same from the mongo shell of the machine B. Using the same script on machine A using the MongoDB of machine B fails on the "$lt" operation.

We remove completelly php5 as well as the php5-mongo and the issue remains after the re-installation.

Important Note: When we execute from php5 the "$lt" range query using smaller Collection (5 reccords instead of 10M) the query is executed normaly.

Could this be an issue due the Large collection size on a small Virtual Machine?. The "$lt" returns instantly like it did not even check the results. No error is returned just an empty cursor.

(The query column has an index and returns quick the results from the mongo shell)

Example:

This is the MongoShell Query:

db.BusData.find({"timestamp":{"$lt":1434037467000000000}})

Returns many objects! (42616452)

The code from the PHP:

$dateFrom=$dateFrom=1433036880000;
$dateTo=1434037467000000000;
$mongoHost = "XXX.XXX.XXX.XX";
$mongoName = 'BusISA_DB'; 
// Connect to test database  
$m = new MongoClient("mongodb://$mongoHost");  
$db = $m->$mongoName;
// select the collection  
$collection = $db->BusData;
$rangeQuery = array('timestamp' => array('$lt' => $dateTo));
$cursor = $collection->find($rangeQuery);
echo json_encode($rangeQuery);
//Output:{"timestamp":{"$lt":1434037467000000000}}
foreach($cursor as $document) {
        print_r($document);
}

Returns empty result. The query printed is : {"timestamp":{"$lt":1434037467000000000}} Using the dateFrom and the "$gt" returns many results. dateFrom<

Solution!:

It turned out that there was an issue with the php5-mongo driver that I was using on the Machine A.

On the Machine B that I refer in the Question the MongoDB driver described on http://docs.mongodb.org/ecosystem/drivers/php/ was used.

After installing the above driver on the Machine A the lt opperator was working. Probably this is a bug of the php5-mongo driver.

From what you've described it certainly sounds like an issue with memory due to a large query (although MongoClient uses cursors so it shouldn't normally be a problem). Make sure you've got error reporting turned on in PHP and check your logs. Also check that your query isn't throwing a MongoException . Failing that you can try different queries and so if you can pinpoint what the tipping point for the failures is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM