2

I have a string converted to MongoDate using

$dateAdded = new MongoDate(strtotime("$time"));
echo "date ".$dateAdded;

So my $dateAdded now looks like 0.00000000 1482505458. (Here 1482505458 is the number of seconds and 0.00000000 is in microseconds.)

Now, I have a document in my collection which has sec = 1482442458 and usec = 622000.

 Array ( [_id] => Array ( [addedtime] => MongoDate Object ( [sec] => 1482505458 [usec] => 622000 ) )

How do I write a query which will only match the seconds field?

Update:

Hers's what I've been trying out :

$date = "2016-12-23 15:04:18";
$m = new MongoClient("mongodb://172.29.0.186:27017");
$dateAdded = new MongoDate(strtotime("+330 minutes",strtotime("$time")));
echo " date ".$dateAdded;

$key = array('$and' => array(array('_id.createdby' => "$user"), array('_id.addedtime' => "$dateAdded")));
$collectionCount->find($key);

And it should match :

 Array ( [_id.createdby] => 10006161 ) Array ( [_id] => Array ( [addedtime] => MongoDate Object ( [sec] => 1482505458 [usec] => 622000 ) [empname] => Praveen Valecha [createdby] => 10006161 ) )
Anubhav
  • 147
  • 1
  • 13
  • The MongoDate object will automatically query the sec property since that is how it is stored internally in MongoDB (/1000). When you echo out you just do `$date->toDateTime()->format('d/m/y')` or something – Sammaye Dec 29 '16 at 08:11
  • But `$collection->find(array('_id.addedtime' => "$dateAdded"));` isn't returning the expected result. – Anubhav Dec 29 '16 at 08:17
  • What is it returning? – Sammaye Dec 29 '16 at 08:48
  • @Sammaye It's not returning anything. I presume that's because there's no such document in the collection which matches the criteria (`0.00000000 1482505458`). – Anubhav Dec 29 '16 at 09:49
  • `0.00000000 1482505458` is not what's used in the query itself, it is merely the serialised string representation of the two properties in your object – Sammaye Dec 29 '16 at 10:43
  • Then how do you propose I query it? – Anubhav Dec 29 '16 at 10:45
  • What you are dong right now should work, can you post a document that should match? – Sammaye Dec 29 '16 at 10:50
  • You see when I run code to output the value of strtotime I get a different value to your document `1482543258`, which means you probably need to use the $gt and $lt operators to get a time range – Sammaye Dec 29 '16 at 11:10
  • @Sammaye I'm not sure but maybe that's because of difference in our timezones which Mongo doesn't adjust itself. Try adding/subtracting your timezone difference from GMT in place of `+330 minutes` – Anubhav Dec 29 '16 at 11:13
  • My timezone is ahead by 5 hours and 30 minutes, hence +330 minutes – Anubhav Dec 29 '16 at 11:15

1 Answers1

0

It's not a perfect solution but it's working for me and should work for most of the people who want to avoid searching for time as accurate as in microseconds.

Here's the part of code I modified from the one asked in the question:

I created a variable with time one minute greater than the time I'm searching for.

$dateAdded = new MongoDate(strtotime("+330 minutes",strtotime("$time")));
echo " dateAdded ".$dateAdded;
$dateFinal = new MongoDate(strtotime("+331 minutes",strtotime("$time")));
echo " dateFinal ".$dateFinal;

Then I used $gte and $lt to query for documents within a date range as suggested by @Sammaye.

$key = array('_id.addedtime' =>  array('$gte' => $dateAdded, '$lt' => $dateFinal));

NOTE : You can also add +1 second to $dateFinal in place of adding an extra minute to achieve more accurate results.

Anubhav
  • 147
  • 1
  • 13