BSON Timestamp C Driver

421 views
Skip to first unread message

jdill

unread,
Jun 10, 2011, 9:16:45 PM6/10/11
to mongodb-user
I know you can create a timestamp serverside by passing the empty
object like so:

bson_timestamp_t ts = {0};
bson_append_timestamp( &rtbb, "ts", &ts );

Doing this on one of the first fields generates the timestamp
serverside which is awesome.

Now, I am looking for a way to update that timestamp (serverside)
without recreating or having to totally upsert the record. I tried
doing $set but it just creates a 0_0 timestamp.

// does not work
bson_buffer_init( &rtcond );
bson_append_string( &rtcond, "_id", id);
bson_buffer_init(&rtbb);
bson_buffer * sub = bson_append_start_object(&rtbb, "$set");
bson_append_timestamp( &rtbb, "ts", &ts );
bson_append_finish_object(sub);
bson_from_buffer(&rtb, &rtbb);
bson_from_buffer(&cond, &rtcond);
mongo_update( conn, tagtable, &cond, &rtb, 0)

Any tricks to make this happen?

Thanks,

Jeremy

Kyle Banker

unread,
Jun 13, 2011, 12:08:10 PM6/13/11
to mongodb-user
This isn't a C-driver issue per se.

BSON timestamp is used internally, and the server does indeed set the
current time when you insert a 0-valued timestamp.

If you want to update the timestamp, however, you'll have to do it
client side. The server doesn't do this. You might consider filing a
feature request, but again, the feature is primarily used for internal
purposes (e.g., sharding, replication).

jdill

unread,
Jun 16, 2011, 5:23:22 PM6/16/11
to mongodb-user
Thanks Kyle, I do notice that I have the same issue with php driver
too, so I realize it's not C driver, so this post might be mis-
titled.

It's not ideal, but I am able to re-create the stamp server-side by
fetching the entire record, and then re-upserting it with a null
timestamp again. $set doesn't work, but full update does recreate the
stamp serverside.

I may try to submit a feature request. To be honest, the server side
autostamping is such a tremendous feature for what I am doing that I
am surprised other's aren't voicing interest. Basically, it's the
only dependable way to allow for polling of 'new' or 'changed' records
on a non-capped collection (that gets written to by many clients).
You can grab records from a collection with any query filtering,
sorted by this server timestamp. Then, on next request, go back and
get all records where 'ts' > last 'ts' received. I can think of many
other uses for this, such as a message bus, etc. Capped collections
are too restrictive for my needs and I struggled with them for a long
time before finding this (you may have recollection of my many posts
in the past on this subject).

Thanks,

Jeremy
Reply all
Reply to author
Forward
0 new messages