0

I want to search for records between two dates. Format of dates are in 'YYYY-MM-DD'. Problem is that when I submit form I got no data.

Does someone knows where is the problem?

Here is example of MongoDB record:

"id": 3,
    "date": "2015-07-19",
    "name": "StressTest",
    "first_test": 4279.558451,
    "second_test": 4296.838515,
    "third_test": 825.446594,
    "min": 2.827739,
    "max": 6071.036922,
    "station": "Alpha1"
}

and here is code that I am using:

$m = new MongoClient("mongodb://root:toor@localhost:55732/db");
$db = $m->$dbname;
$results;
$collection = $db->db;
if(isset($_POST['submit'])){
    $array = array();
    $array['telescope']=$_POST['teleskop'];
    $lowdate=$_POST["datepickerOD"];
    $highdate=$_POST["datepickerDO"];
    echo '{ date: {$gt: '.$lowdate.', $lt: '.$highdate.' } }';
    $array = json_decode('{ date: {$gt: '.$lowdate.', $lt: '.$highdate.' } }');

    $results = $collection->find($array)->limit(500);
    var_dump($results);

}
jureispro
  • 1,342
  • 5
  • 22
  • 43
  • Why are you decoding JSON and not just writing this in native code? You also really should have BSON `Date` objects and not strings here as well. And I don't know how you got to post a question with almost exactly the same title as: [Find objects between two dates MongoDB](http://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb). Chances are your `$lowdate` and `$highdate` are not actually "stringifying" as you are expecting they would. And your present data does mean that these still need to be strings. – Blakes Seven Aug 18 '15 at 08:40
  • @BlakesSeven How can I cast it to date? I can cast search dates to Date object but how can I cast mongoDB date record to date object? – jureispro Aug 18 '15 at 10:16
  • Well there should be several ways to parse the present strings and store them as dates. You can either write it in PHP or simply use something like the mongo shell to do a "one off" conversion. Right *now* though your problem seems to be with the variables you are declaring outside of the code you are showing here. Which is also what I mentioned before. If you cannot work that out, then at least edit your question to show how you are declaring those variables. Then it should become clear to someone, what the problem exactly is. – Blakes Seven Aug 18 '15 at 10:25

0 Answers0