Mirodil
Mirodil

Reputation: 2329

How query to Timestamp filed from C# Mongodb Driver?

i have collection named "oplog.rs" in the local database and it has "ts" with Timestamp field type. The data stored as the following:

{ 
    "ts" : Timestamp(1374672101000, 1), 
    "h" : NumberLong(0), 
    "v" : 2, 
    "op" : "n",
    "ns" : "", 
    "o" : { "msg" : "initiating set" } 
}

How can i query to Timestamp field from C# driver?

here are data in the "opslog" collection:

{ "ts" : Timestamp(1375198013000, 1), "h" : NumberLong("-4582872941246748867"),"v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198012000, 5), "h" : NumberLong("2189325868256500260"), "v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198012000, 4), "h" :NumberLong("-4649577771618475915"),"v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ...} }
{ "ts" : Timestamp(1375198012000, 3), "h" : NumberLong("9039523732738063755"), "v" : 2, "op" : "i", "ns" :"audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198012000, 2), "h" :NumberLong("8432711896209033544"), "v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198012000, 1), "h" : NumberLong("8059694251326474562"), "v" : 2, "op" : "i", "ns" :"audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198011000, 5), "h" : NumberLong("8581369985048440971"), "v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198011000, 4), "h" : NumberLong("-1142902968129950222"),"v" : 2, "op" : "i", "ns" :"audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198011000, 3), "h" : NumberLong("-4104381148312926566"),"v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198011000, 2), "h" : NumberLong("-1413662537726747267"),"v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198011000, 1), "h" :NumberLong("-1031425920752689991"),"v" : 2, "op" : "i", "ns" : "audit.search", "o" : { ... } }
{ "ts" : Timestamp(1375198010000, 1), "h" : NumberLong("12432179788117176"), "v" : 2, "op" : "i", "ns" :"audit.search", "o" : { ... } }

i have 2 query:

{ "ts" : { "$lte" : NumberLong("5906428099062398977") } }

and

{ "ts" : { "$gte" : NumberLong("5906428099062398977") } }

The first query is return results but the second query does not return any result. Why is "$gte" operator not working?

Upvotes: 0

Views: 3889

Answers (1)

WiredPrairie
WiredPrairie

Reputation: 59763

You'll need to use the BsonTimestamp class (docs).

public class OpsLog {
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    [BsonElement("ts")] // use the abbreviation in serialization
    public BsonTimestamp TimeStamp{ get; set; }
    // etc...
}

You can query for it by creating an instance using the constructor passing in a long number representing the timestamp:

var query = Query.EQ("ts", new BsonTimeStamp(timestampValue));
var cursor = myCollection.FindAs<OpsLog>(query);
foreach(var log in cursor) {
    // etc...
}

Upvotes: 3

Related Questions