PHP Mongo: Querying By _id timestamp returns empty-handed

1,056 views
Skip to first unread message

Van

unread,
Aug 19, 2010, 7:41:19 PM8/19/10
to mongodb-user
I created some PHP code that converts a date into a MongoId:

$start_datetime = new DateTime("19-08-2010");
$ts = str_pad(dechex($start_datetime->getTimestamp()), 24, "0",
STR_PAD_RIGHT)
$timestamp = new MongoId("$ts");

But if I query using this query:

$query = array("_id"=>array('gt'=>$timestamp));
$result = $collection->find($query)->count()

The $result is always zero even if there are documents that match in
the collection. A python version of the script works fine. Am I
doing something wrong in the querying process for _id? Please let me
know. Thanks for your time.

Best Regards,
Van

Van

unread,
Aug 19, 2010, 7:46:48 PM8/19/10
to mongodb-user
PHP version 5.3.1
PHP Mongo version 1.0.8

Operating Systems affected: OSX 10.6.4, CentOS 5.2

Mardix

unread,
Aug 19, 2010, 8:37:43 PM8/19/10
to mongodb-user
Well to start off, just leave _id alone.

_id is inserted automatically by mongo. If you want your own id, you
can create your own ID field and make it whatever you want.

And why making it so complicated? You can create a 'timestamp' field
in each document and can be queried from there.

Or better yet, if you know the _id, you can query _id that is $gt the
_id you want.

For Mongo sake, keep it simple son. Mongo was meant to be easy, not
complicated.

That's my $0.25 .

Scott Hernandez

unread,
Aug 19, 2010, 9:20:46 PM8/19/10
to mongod...@googlegroups.com
Mardix: Perhaps you should read the format for an ObjectId (the default _id type, if a value is not specified on insert): http://www.mongodb.org/display/DOCS/Object+IDs -- it leads out with a timestamp (of seconds precision).
 
If seconds is good then there is no need to create another field, or another index.

Van, I don't know PHP, or the PHP driver, well enough to answer your question, but I'm sure someone will...


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Kristina Chodorow

unread,
Aug 20, 2010, 12:43:43 PM8/20/10
to mongod...@googlegroups.com
You need to use $gt, not gt:

$query = array("_id"=>array('$gt'=>$timestamp));

If I change that, it works for me.

Van

unread,
Aug 23, 2010, 1:13:52 PM8/23/10
to mongodb-user
Ooops, thanks Kristina. Sorry I can't believe I didn't see that.

On Aug 20, 9:43 am, Kristina Chodorow <krist...@10gen.com> wrote:
> You need to use $gt, not gt:
>
> $query = array("_id"=>array('$gt'=>$timestamp));
>
> If I change that, it works for me.
>
> On Thu, Aug 19, 2010 at 9:20 PM, Scott Hernandez
> <scotthernan...@gmail.com>wrote:
>
>
>
> > Mardix: Perhaps you should read the format for an ObjectId (the default _id
> > type, if a value is not specified on insert):
> >http://www.mongodb.org/display/DOCS/Object+IDs-- it leads out with a
> > timestamp (of seconds precision).
>
> > If seconds is good then there is no need to create another field, or
> > another index.
>
> > Van, I don't know PHP, or the PHP driver, well enough to answer your
> > question, but I'm sure someone will...
>
> > On Thu, Aug 19, 2010 at 5:37 PM, Mardix <mcx2...@gmail.com> wrote:
>
> >> Well to start off, just leave _id alone.
>
> >> _id is inserted automatically by mongo. If you want your own id, you
> >> can create your own ID field and make it whatever you want.
>
> >> And why making it so complicated? You can create a 'timestamp' field
> >> in each document and can be queried from there.
>
> >> Or better yet, if you know the _id, you can query _id that is $gt the
> >> _id you want.
>
> >> For Mongo sake, keep it simple son. Mongo was meant to be easy, not
> >> complicated.
>
> >> That's my $0.25 .
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "mongodb-user" group.
> >> To post to this group, send email to mongod...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/mongodb-user?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> > .

Van

unread,
Aug 23, 2010, 1:20:28 PM8/23/10
to mongodb-user
Thanks anyway Scott.

On Aug 19, 6:20 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> Mardix: Perhaps you should read the format for an ObjectId (the default _id
> type, if a value is not specified on insert):http://www.mongodb.org/display/DOCS/Object+IDs-- it leads out with a
> timestamp (of seconds precision).
>
> If seconds is good then there is no need to create another field, or another
> index.
>
> Van, I don't know PHP, or the PHP driver, well enough to answer your
> question, but I'm sure someone will...
>
>
>
> On Thu, Aug 19, 2010 at 5:37 PM, Mardix <mcx2...@gmail.com> wrote:
> > Well to start off, just leave _id alone.
>
> > _id is inserted automatically by mongo. If you want your own id, you
> > can create your own ID field and make it whatever you want.
>
> > And why making it so complicated? You can create a 'timestamp' field
> > in each document and can be queried from there.
>
> > Or better yet, if you know the _id, you can query _id that is $gt the
> > _id you want.
>
> > For Mongo sake, keep it simple son. Mongo was meant to be easy, not
> > complicated.
>
> > That's my $0.25 .
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> > .
Reply all
Reply to author
Forward
0 new messages