Search code examples
c#mongodb-.net-driver

How do I represent an "empty object" for MongoDB .NET Driver?


I am trying to retrieve index statistics using the MongoDB .NET Driver.

I have tried the following variations of my pipeline

  • var statsPipeline = new[] { new BsonDocument(new BsonElement("$indexStats", BsonNull.Value)) };
  • var statsPipeline = new[] { new BsonDocument { {"$indexStats", "" } } };
  • var statsPipeline = new[] { new BsonDocument { {"$indexStats", null } } };
  • var statsPipeline = new[] { new BsonDocument { {"$indexStats", BsonNull.Value } } };
  • var statsPipeline = new[] { new BsonDocument { {"$indexStats", "{ }"} } };

which is passed to the query

var stats = await db
    .GetCollection<BsonDocument>("CollectionName")
    .AggregateAsync<BsonDocument>(statsPipeline);

With the exception of the one containing null, which resulted in an ArgumentNullException, I have received the exception

MongoDB.Driver.MongoCommandException: Command aggregate failed: The $indexStats stage specification must be an empty object.

How do I change my query such that the $indexStats stage specification is indeed an empty object?


Solution

  • Ok, this one worked:

    var statsPipeline = new[] { new BsonDocument(new BsonElement("$indexStats", new BsonDocument())) };