查找和FindAsync的区别 [英] Difference between Find and FindAsync

查看:2904
本文介绍了查找和FindAsync的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个非常简单的查询刚刚根据其唯一的ID从集合中获取的文件。一些frusteration后(我是新来蒙戈和异步/等待编程模型),我想通了这一点:

I am writing a very, very simple query which just gets a document from a collection according to its unique Id. After some frusteration (I am new to mongo and the async / await programming model), I figured this out:

IMongoCollection<TModel> collection = // ...
FindOptions<TModel> options = new FindOptions<TModel> { Limit = 1 };
IAsyncCursor<TModel> task = await collection.FindAsync(x => x.Id.Equals(id), options);
List<TModel> list = await task.ToListAsync();
TModel result = list.FirstOrDefault();
return result;



它的工作原理,太棒了!但是,我总是看到引用到查找的方法,和我合作了这一点:

It works, great! But I keep seeing references to a "Find" method, and I worked this out:

IMongoCollection<TModel> collection = // ...
IFindFluent<TModel, TModel> findFluent = collection.Find(x => x.Id == id);
findFluent = findFluent.Limit(1);
TModel result = await findFluent.FirstOrDefaultAsync();
return result;



事实证明,这也的作品,太棒了!

As it turns out, this too works, great!

我敢肯定有,我们有两种不同的方式来实现这些结果的一些重要原因。什么是这些方法之间的区别,为什么我要选择一个比其他?

I'm sure that there's some important reason that we have two different ways to achieve these results. What is the difference between these methodologies, and why should I choose one over the other?

推荐答案

所不同的是语法。
查找 FindAsync 既可以建立异步查询具有相同的性能,只有

The difference is in a syntax. Find and FindAsync both allows to build asynchronous query with the same performance, only

FindAsync 收益光标这一次 不加载的所有文件和为您提供界面来检索文件一个个从DB光标。这是有帮助的情况下,当查询结果是巨大的。

FindAsync returns cursor which doesn't load all documents at once and provides you interface to retrieve documents one by one from DB cursor. It's helpful in case when query result is huge.

查找为您提供更简单的语法通过方法 ToListAsync 它里面检索光标文件和一次。

Find provides you more simple syntax through method ToListAsync where it inside retrieves documents from cursor and returns all documents at once.

这篇关于查找和FindAsync的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆