Teo*_*ahi 5 .net c# mongodb mongodb-csharp-2.0 mongodb-.net-driver
我使用的是MongoDB.Driver 2.0.0.有没有办法看到从linq到MongoDB生成的脚本?
例如,我的查询是:
IFindFluent<ProductMapping, ProductMapping> findFluent = Collection.Find(
x => hashValues.Contains(x.UrlHash) && x.ProductTopic == topicId);
Run Code Online (Sandbox Code Playgroud)
如何(或更复杂的查询)在MongoDB shell中表示?
i3a*_*non 12
编辑:从驱动程序的2.0.1版本开始,FindFluent返回的对象IMongoCollection.Find具有适当的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);
Run Code Online (Sandbox Code Playgroud)
输出将是:
find({ "UrlHash" : { "$in" : [4, 5, 6, 7, 8] }, "ProductTopic" : 200 }, { "UrlHash" : 1, "_id" : 0 }).
sort({ "ProductTopic" : -1 }).
skip(6).
limit(7).
maxTime(1000)
Run Code Online (Sandbox Code Playgroud)
好吧,你已经知道你正在做一个查找,所以我假设你想知道查询是什么样的.
您可以使用IFindFluent.Filter以下代码直接从代码中轻松完成:
BsonDocument filterDocument = findFluent.Filter.Render(
collection.DocumentSerializer,
collection.Settings.SerializerRegistry);
Console.WriteLine(filterDocument);
Run Code Online (Sandbox Code Playgroud)
在你的情况下,输出(取决于hashValues和topicId课程):
{ "UrlHash" : { "$in" : [4, 5, 6, 7, 8, 9] }, "ProductTopic" : 200 }
Run Code Online (Sandbox Code Playgroud)
请参阅 i3arnon 的答案,了解使用该方法的客户端方法Render()通常更容易。
您可以使用集成的 mongodb profiler查看数据库实际收到的内容:
db.setProfilingLevel(2); // log every request
// show the requests that mongodb has received, along with execution stats:
db.system.profile.find().pretty()
Run Code Online (Sandbox Code Playgroud)
或者,您可以单步执行驱动程序的源代码并等待它实际创建消息。然而,这需要从源代码编译驱动程序,据我所知。
| 归档时间: |
|
| 查看次数: |
1997 次 |
| 最近记录: |