MongoDB / C# - Inserting document with an automatically populated timestamp

2,126 views
Skip to first unread message

Sam Martin

unread,
Feb 1, 2012, 9:56:16 AM2/1/12
to mongodb-user
I'm trying to insert into a MongoDB collection.

I have a data model containing an Id (ObjectId) and a Timestamp (long)
as the first two properties.

From here (http://www.mongodb.org/display/DOCS/Timestamp+data+type -
second para), i understand that if these are both null, they should
get automatically populated?

When Saving the entity, the ObjectId (Id/_id) column is being set,
although the timestamp remains null. Is there something special i need
to do set set this?

I've tried:

    newdoc= Update.Replace(doc.ToBsonDocument().Set("Timestamp", new
BsonJavaScript("new Timestamp()")));
    db.mydocs.Save(newdoc);
But then get "GetDocumentId method cannot be called on a
UpdateWrapper." exception.

Can anybody point me in right direction?

Thanks in advance

Sam

Robert Stam

unread,
Feb 1, 2012, 11:28:35 AM2/1/12
to mongod...@googlegroups.com
You probably need to set your BsonTimestamp value to:

new BsonTimestamp(0)

instead of to C# null in order to get the server to fill it out for you. If the server only sees a null it won't know that it's a Timestamp.

Also, automatic generation of _id and timestamps only applies to Insert, not to Update. It might apply to Save but only if Save ends up calling Insert instead of Update.


Sam

--
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.


Sam Martin

unread,
Feb 1, 2012, 1:32:30 PM2/1/12
to mongodb-user
Thanks Robert, that makes sense.

Do you know how to do an update so that the MongoDB server assigns a
timestamp opposed to doing it on the client?

thanks in advance
sam

Robert Stam

unread,
Feb 1, 2012, 1:38:49 PM2/1/12
to mongod...@googlegroups.com
As far as I know the ability of having the server assign the Timestamp value for you is only available on an Insert.

Sam Martin

unread,
Feb 1, 2012, 2:30:19 PM2/1/12
to mongodb-user
hi robert,

I've seen you can do this in mongo shell:

> db.foo.insert( { x : 1, y : new Timestamp() } )

can you do ...
> db.foo.update( { x : 1, y : new Timestamp() } ) ...?


and how would you do it from C# driver?

thanks for you help robert.

sam

Robert Stam

unread,
Feb 1, 2012, 3:09:26 PM2/1/12
to mongod...@googlegroups.com
Your update statement is missing a query.

So it appears that Update will replace an empty timestamp, but ONLY IF you are replacing the entire document with a new one.

To replace an entire document use the Update.Replace modifier in C#.

var query = ...;
var update = Update.Replace(document);
collection.Update(query, update);

Not sure if the server behavior in this case is deliberate or accidental. Might be a bit dangerous to rely on this behavior.

Sam Martin

unread,
Feb 1, 2012, 3:41:45 PM2/1/12
to mongodb-user
Yes, youre right. Sorry about that. Ok, so I can set the Timestamp
property to BsonTimestamp(0) would trigger the db to assign a new
timestamp value, but on for an insert or update where I replace the
document like in your example.

Could I have this as a long property in my c# mapped class and do
something like...

[BsonRepresentation(BsonType.BsonTimestamp)]
public long Timestamp { get; set; }


Sorry for all the questions, I'm really grateful for your replies/
advice.

Sam

Robert Stam

unread,
Feb 2, 2012, 12:11:33 AM2/2/12
to mongod...@googlegroups.com
No, you can't use BsonTimestamp as the representation for a long value.
Reply all
Reply to author
Forward
0 new messages