使用C#仅获取MongoDB中的指定字段 [英] Get only a specified field in MongoDB with C#
本文介绍了使用C#仅获取MongoDB中的指定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我第一次使用MongoDB.
first time i'm using MongoDB.
我已经阅读了以下示例:
I have read this example:
从年龄为33岁的用户中选择a,b db.users.find({age:33},{a:1,b:1})
SELECT a,b FROM users WHERE age=33 db.users.find({age:33}, {a:1,b:1})
但是我不能将其翻译成C#.谁能帮我吗?
But I can't translate it into C#. Can anyone help me?
推荐答案
您可以使用MongoCursor
类的SetFields
方法执行此操作,下面是完整示例:
You can do it using SetFields
method of MongoCursor
class, below full example:
var server = MongoServer.Create(connectionString);
var db = _server.GetDatabase("dbName");
db.GetCollection("users");
var cursor = Photos.FindAs<DocType>(Query.EQ("age", 33));
cursor.SetFields(Fields.Include("a", "b"));
var items = cursor.ToList();
这篇关于使用C#仅获取MongoDB中的指定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文