如何为MongoDB .NET驱动程序表示“空对象”?

时间:2019-06-20 14:42:12

标签: c# mongodb-.net-driver

我正在尝试使用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阶段规范确实为空对象?

1 个答案:

答案 0 :(得分:1)

好的,这个工作了:

import concurrent.futures

from concurrent.futures import FIRST_COMPLETED

def main():
    allow_new_work = True  # Set to False to indicate we'll no longer allow new work
    myfancyclasses = [MyFancyClass('Fancy Dan'), ...] # define your initial MyFancyClass instances here
    with concurrent.futures.ProcessPoolExecutor() as executor:
        remaining_futures = {executor.submit(fancy.do_something)
                             for fancy in myfancyclasses}
        while remaining_futures:
            done, remaining_futures = concurrent.futures.wait(remaining_futures,
                                                              return_when=FIRST_COMPLETED)
            for fut in done:
                result = fut.result()
                # Do stuff with result, maybe submit new work in response

            if allow_new_work:
                if should_stop_checking_for_new_work():
                    allow_new_work = False
                    # Let the workers exit when all remaining tasks done,
                    # and reject submitting more work from now on
                    executor.shutdown(wait=False)
                elif has_more_work():
                    # Assumed to return collection of new MyFancyClass instances
                    new_fanciness = get_more_fanciness()
                    remaining_futures |= {executor.submit(fancy.do_something)
                                          for fancy in new_fanciness}
                    myfancyclasses.extend(new_fanciness)