top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Does anybody know how to get current server time using mongodb c# driver?

0 votes
652 views
Does anybody know how to get current server time using mongodb c# driver?
posted Jun 6, 2016 by Dewang Chaudhary

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

var database = client.GetDatabase("databaseName");
var collection = database.GetCollection("collectionName");
var newDocument = new BsonDocument { { "timestamp", DateTime.UtcNow} };
collection.InsertOne(newDocument);

Which should generate a document below in MongoDB:

{ "_id" : ObjectId("5761046d18bbc402248650a1"), "timestamp" : ISODate("2016-06-15T07:31:57.235Z") }

The snippet above is for MongoDB Driver v2.2.x and MongoDB 3.2.x.

answer Jun 25, 2019 by Rushabh Verma R.
Similar Questions
+1 vote

I am trying to get all the collection names from MongoDb server using C# code using db.GetCollectionNames() method.

I have 12 collections in my database, but the above method returns an empty list. I have verified that I am referring to the correct database. What am I doing wrong? Or if there is an alternate way?

0 votes

I want to connect a particular MONGODB database to c# application using Web.config.
Please tell me how to do it?

0 votes

is it possible to create views using the Node.js MongoDB Driver (3.0)? There is no db.createView() in the drivers API reference.

+1 vote

Can we connect to MongoDB Server 2.6.12 using Java Driver version 0.11? We have a constraint of upgrading Java Driver version which would require huge change in our client code. We use JDK 8 for compilation of our project. We would like to know whether it is possible to connect to MongoDB Server 2.6.12 or MongoDB Server 3.0 using Java Driver version 0.11 if we only use the limited set of features (not the functionalities that were introduced in Server version 2.6.12 or 3.0).

+2 votes

Considering working with a shard replica-set cluster, some emergency such as re-pick primary node will happen and maybe we can retry the command several times for waiting for recover of mongo.

Is there anyone know when use c driver api such as mongoc_collection_update, mongoc_collection_commond, mongoc_collection_insert, which error codes tell me that I can retry command for making the application more robust.

...