MongoDB C# 驱动程序 - 对 _id 执行 "IN" 查询的最快方法
MongoDB C# Driver - Fastest way to perform an "IN" query on _id
我正在尝试根据 ID 在特定 ID 集合中的项目从集合中获取值。
我当前构建过滤器的代码是:
IEnumerable<string> IDList;
using (var enumerator = IDList.GetEnumerator())
{
if (enumerator.MoveNext() == false) return null; // empty collection
// take the first key
var key = enumerator.Current;
filter = Builders<MyClass>.Filter.Eq(p => p.Key, key);
// take all the other keys
while (enumerator.MoveNext())
{
var innerKey = enumerator.Current;
filter = filter | Builders<MyClass>.Filter.Eq(p => p.Key, innerKey);
}
}
然后我获取项目的代码是:
List<MyClass> values = new List<MyClass>();
using (var cursor = await MyCollection.FindAsync(filter))
{
while (await cursor.MoveNextAsync())
{
values.AddRange(cursor.Current);
}
}
这段代码的性能似乎很差,而且我确信必须有更快的方法,因为 MongoDB 应该有很好的性能......更不用说我正在查询一个索引字段,它应该使查询非常快。我该怎么做才能以异步方式和同步方式加快速度?通过一些谷歌搜索,我发现查询集合的方法有很多种,但我不确定哪种方法最适合我的特定情况。
运行 RoboMongo 中的这个查询需要 0.02 秒,而 运行 在 C# 中 MongoDb.Driver 需要整整一秒,有时甚至更长,我不确定为什么。
提前致谢。
简单的“$in”查询怎么样?
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class MyClass
{
public ObjectId Id;
public string Key;
}
public class Program
{
static void Main(string[] args)
{
IEnumerable<string> ids = new [] { "a", "b", "c" };
var collection = new MongoClient().GetDatabase("test").GetCollection<MyClass>("test");
foreach (var id in ids)
{
collection.InsertOne(new MyClass { Key = id });
}
// here comes the "$in" query
var filter = Builders<MyClass>.Filter.In(myClass => myClass.Key, ids);
// sync
List<MyClass> values = collection.Find(filter).ToList();
// async
var queryTask = collection.FindAsync(filter);
values = GetValues(queryTask).Result;
Console.ReadLine();
}
private static async Task<List<MyClass>> GetValues(System.Threading.Tasks.Task<IAsyncCursor<MyClass>> queryTask)
{
var cursor = await queryTask;
return await cursor.ToListAsync<MyClass>();
}
}
}
我正在尝试根据 ID 在特定 ID 集合中的项目从集合中获取值。
我当前构建过滤器的代码是:
IEnumerable<string> IDList;
using (var enumerator = IDList.GetEnumerator())
{
if (enumerator.MoveNext() == false) return null; // empty collection
// take the first key
var key = enumerator.Current;
filter = Builders<MyClass>.Filter.Eq(p => p.Key, key);
// take all the other keys
while (enumerator.MoveNext())
{
var innerKey = enumerator.Current;
filter = filter | Builders<MyClass>.Filter.Eq(p => p.Key, innerKey);
}
}
然后我获取项目的代码是:
List<MyClass> values = new List<MyClass>();
using (var cursor = await MyCollection.FindAsync(filter))
{
while (await cursor.MoveNextAsync())
{
values.AddRange(cursor.Current);
}
}
这段代码的性能似乎很差,而且我确信必须有更快的方法,因为 MongoDB 应该有很好的性能......更不用说我正在查询一个索引字段,它应该使查询非常快。我该怎么做才能以异步方式和同步方式加快速度?通过一些谷歌搜索,我发现查询集合的方法有很多种,但我不确定哪种方法最适合我的特定情况。
运行 RoboMongo 中的这个查询需要 0.02 秒,而 运行 在 C# 中 MongoDb.Driver 需要整整一秒,有时甚至更长,我不确定为什么。
提前致谢。
简单的“$in”查询怎么样?
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class MyClass
{
public ObjectId Id;
public string Key;
}
public class Program
{
static void Main(string[] args)
{
IEnumerable<string> ids = new [] { "a", "b", "c" };
var collection = new MongoClient().GetDatabase("test").GetCollection<MyClass>("test");
foreach (var id in ids)
{
collection.InsertOne(new MyClass { Key = id });
}
// here comes the "$in" query
var filter = Builders<MyClass>.Filter.In(myClass => myClass.Key, ids);
// sync
List<MyClass> values = collection.Find(filter).ToList();
// async
var queryTask = collection.FindAsync(filter);
values = GetValues(queryTask).Result;
Console.ReadLine();
}
private static async Task<List<MyClass>> GetValues(System.Threading.Tasks.Task<IAsyncCursor<MyClass>> queryTask)
{
var cursor = await queryTask;
return await cursor.ToListAsync<MyClass>();
}
}
}