[CSHARP-672] Derived classes can't put timestamp in second position for server to automatically fill in the value Created: 01/Feb/13  Updated: 11/Mar/19  Resolved: 06/Apr/15

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.7
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

If a document being saved to the server has an _id field in the first position and a zero valued BsonTimestamp (name doesn't matter) in the second position then the server fills in the value of the timestamp field automatically.

But when using polymorphism with the C# driver this doesn't work for derived classes.

Given the following classes:

public class C
{
    public int Id;
    public BsonTimestamp Timestamp;
}
 
public class D : C
{
    public int X;
}

when an instance of D is serialized (assuming the nominal type is C) the result is:

{ _id : 1, _t : "D", Timestamp : { $timestamp : NumberLong(0) }, X : 1 }

but since the driver inserted the _t discriminator field in the second position the server no longer sees the Timestamp field and doesn't fill the value in for you.

The driver always puts the _t field in the second position so it doesn't have to scan too much of the document to find it, but in this case it should put it in the third position so the server can see the Timestamp in the second position.



 Comments   
Comment by Robert Stam [ 22/Sep/14 ]

Removed from Sprint 8. It's unclear whether we even want to implement this. The server team seems to recommend using an upsert with $currentDate instead.

Comment by David Wagner [ 09/Aug/13 ]

This is not the only way to break the auto-generation of timestamps: see SERVER-10481, which I think is a server-side-only problem.

Comment by Kirill Davletkildeev [ 09/Mar/13 ]

Sounds perfectly reasonable. Still when the extra convention/attribute is not explicitly applied, discriminators should not change the timestamps autogeneration.

Comment by Robert Stam [ 08/Mar/13 ]

We're rescheduling this ticket to 2.0. One reason is that it is not yet clear exactly how this should be supported. For example, it probably shouldn't be required for the auto timestamp field to be the first or second field in the class declaration, and it might even be defined in one of the subclasses.

One possible implementation is to treat it similarly to how the _id field is treated, with attributes and conventions to identify the auto timestamp field.

Here's a sample class declaration that illustrates how an attribute might be used to identify the auto timestamp field:

public class C
{
    public ObjectId Id;
    public int X;
    public int Y;
}
 
public class D : C
{
    [BsonAutoTimestamp] // new attribute, exact name to be decided
    public BsonTimestamp Timestamp;
    public int Z;
}

An instance of D serialized with a nominal type of C (so _t is required) might look like this:

{
    _id : ...,
    Timestamp : new Timestamp(0), // serialized in the second position so server will recognize it
    _t : "D",
    X : 1,
    Y : 2,
    Z : 3
}

Comment by Robert Stam [ 08/Mar/13 ]

Here's a link to the relevant server documentation:

http://docs.mongodb.org/manual/core/document/#timestamps

Comment by Kirill Davletkildeev [ 14/Feb/13 ]

This also should work, so that serializing with and without nominal type works similar:

public class C
{
    public BsonTimestamp Timestamp;
    public int Id;
}
 
public class D : C
{
    public int X;
}
 
_collection.Insert(doc);
// vs
_collection.Insert(typeof(C), doc);

Generated at Wed Feb 07 21:37:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.