【问题标题】:How do I represent an "empty object" for MongoDB .NET Driver?如何为 MongoDB .NET 驱动程序表示“空对象”?
【发布时间】:2019-06-20 14:42:12
【问题描述】:

我正在尝试使用 MongoDB .NET 驱动程序检索索引统计信息。

我已经尝试过我的管道的以下变体

  • 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", "{ }"} } };

传递给查询

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

除了包含null 的那个导致ArgumentNullException 之外,我收到了异常

MongoDB.Driver.MongoCommandException:命令聚合失败:$indexStats 阶段规范必须是空对象。

如何更改我的查询以使 $indexStats 阶段规范确实是一个空对象?

【问题讨论】:

  • 我完全没有使用 MongoDB 的经验,但尝试一个实际的空(匿名输入)对象:new {}。

标签: c# mongodb-.net-driver


【解决方案1】:

好的,这个成功了:

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

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多