How to get MongoDB collection creation date using MongoDB C # driver?

I need to iterate over all the collections in my MongoDB database and get the time when each of the collections was created (I understand that I can get the timestamp of each object in the collection, but I would prefer not to go the route if there is a simpler / more quick method).

This should give you an idea of ​​what I'm trying to do:

MongoDatabase _database;
// code elided
var result = _database.GetAllCollectionNames().Select(collectionName =>
    {
        _database.GetCollection( collectionName ) //.{GetCreatedDate())
    });
+5
source share
2 answers

, MongoDB . , . , - , , :

public static void CreateCollectionWithMetadata(string collectionName)
{
    var result = _db.CreateCollection(collectionName);
    if (result.Ok)
    {
        var collectionMetadata = _db.GetCollection("collectionMetadata");
        collectionMetadata.Insert(new { Id = collectionName, Created = DateTime.Now });
    }
}

, , collectionMetadata. , , , , :

public static DateTime GetCreatedDate(this MongoCollection collection)
{
    var collectionMetadata = _db.GetCollection("collectionMetadata");
    var metadata = collectionMetadata.FindOneById(collection.Name);
    var created = metadata["Created"].AsDateTime;
    return created;
}
+5

" " . "", . ObjectId(), , .

, .

+4

All Articles