编辑:从驱动程序的 2.0.1 版本开始,从 IMongoCollection.Find 返回的 FindFluent 对象具有适当的 ToString,其中包括过滤器,还有投影、排序等(如果相关)。
所以,对于这个:
var findFluent = collection.
Find(x => hashValues.Contains(x.UrlHash) && x.ProductTopic == topicId,
new FindOptions {MaxTime = TimeSpan.FromSeconds(1)}).
Project(x => x.UrlHash).
Sort(Builders<ProductMapping>.Sort.Descending(x => x.ProductTopic)).
Skip(6).
Limit(7);
Console.WriteLine(findFluent);
输出将是:
find({ "UrlHash" : { "$in" : [4, 5, 6, 7, 8] }, "ProductTopic" : 200 }, { "UrlHash" : 1, "_id" : 0 }).
sort({ "ProductTopic" : -1 }).
skip(6).
limit(7).
maxTime(1000)
好吧,您已经知道您正在执行查找,所以我假设您想知道查询的样子。
您可以使用 IFindFluent.Filter 直接在您的代码中轻松地做到这一点:
BsonDocument filterDocument = findFluent.Filter.Render(
collection.DocumentSerializer,
collection.Settings.SerializerRegistry);
Console.WriteLine(filterDocument);
您的情况下的输出(当然取决于hashValues 和topicId):
{ "UrlHash" : { "$in" : [4, 5, 6, 7, 8, 9] }, "ProductTopic" : 200 }