MongoDB中如何检查生存 [英] MongoDB how to check for existence

查看:168
本文介绍了MongoDB中如何检查生存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎么能检查用的MongoDB和C#的对象的存在。

I would like to know how can I check the existence of an object with mongoDB and C#.

我已经找到一种方法来做到这一点,但我不得不使用Linq感谢任何()方法,但我想知道是否有可能做到这一点没有LINQ的?

I've found a way to do it but I had to use Linq thanks to Any() method, but I'd like to know if it's possible to do it without Linq ?

database.GetCollection<ApplicationViewModel>("Applications").Find(Query.EQ("Name", applicationName)).Any()

谢谢你们!

推荐答案

使用的 $算运营商,以避免内存问题,它不加载从数据库文件到内存中:

Use $count operator to avoid memory issues, it not loading documents from database into memory:

int count = items.FindAs<LedgerDocument>(Query.EQ("name", appName)).Count();

if(count > 0)
{
   //then doc exists
}

存在于 MongoDB的可用于identfy,一些领域的文件存在,但你不能传递查询它:

Operator $exists in mongodb can be used to identfy that some field exists in a document, but you can't pass query to it:

database.GetCollection<ApplicationViewModel>("Applications")
                  .Find(Query.Exists("Name", true));

这篇关于MongoDB中如何检查生存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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