【问题标题】:MongoDB and C# Find()MongoDB 和 C# Find()
【发布时间】:2017-03-03 01:43:59
【问题描述】:

我有以下代码,而且我是 mongodb 的新手,我需要帮助来查找集合中的特定元素。

using MongoDB.Bson;
using MongoDB.Driver;
namespace mongo_console    {

public class User    {
    public ObjectId Id { get; set; }
    public string name { get; set; }
    public string pwd { get; set; }
}
class Program    {
    static void Main(string[] args)
    {
        MongoClient client = new MongoClient();
        MongoServer server = client.GetServer();
        MongoDatabase db = server.GetDatabase("Users");
        MongoCollection<User> collection = db.GetCollection<User>("users");

        User user = new User
        {
            Id = ObjectId.GenerateNewId(),
            name = "admin",
            pwd = "admin"
        };
        User user2 = new User
        {
            Id = ObjectId.GenerateNewId(),
            name = "system",
            pwd = "system"
        };
        collection.Save(user);
        collection.Save(user2);

        /*
         * How do I collection.Find() for example using the name
         */
  }
}
}

一旦我找到想要打印的用户,这是否可行或只会返回该位置?如果是这样,我该如何打印?

我看过一些示例 collection.Find(x => x.something) 但我不知道 x 是什么意思

【问题讨论】:

    标签: c# mongodb mongodb-query


    【解决方案1】:

    要查找记录,您可以在 find 中使用 Lambda,例如:

    var results = collection.Find(x => x.name == "system").ToList();
    

    或者,您可以使用与强类型 Lambda 或文本一起使用的构建器:

    var filter = Builders<User>.Filter.Eq(x => x.name, "system")
    

    或者

    var filter = Builders<User>.Filter.Eq("name", "system")
    

    然后像上面那样使用find

    // results will be a collection of your documents matching your filter criteria
    
    // Sync syntax
    var results = collection.Find(filter).ToList();
    
    // Async syntax
    var results = await collection.Find(filter).ToListAsync();
    

    【讨论】:

    • 如果您在.Find(x =&gt; x.name == "system") 内部过滤而不是稍后的.Find().Where(x =&gt; x.name == "system"),您会获得性能提升吗?
    【解决方案2】:
    using MongoDB.Bson;
    using MongoDB.Bson.Serialization.Attributes;
    using MongoDB.Driver;
    using MongoDB.Driver.Builders;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Mongo_console
    {
        class Program
        {
            public static void Main(string[] args)
            {
                MongoClient client = new MongoClient();
                MongoServer server = client.GetServer();
                MongoDatabase db = server.GetDatabase("admin");
                MongoCollection<Book> collection = db.GetCollection<Book>("Book");
    
    
                Book book1 = new Book
                {
                    Id = ObjectId.GenerateNewId(),
                    name = "Reel To Real"
                };
                Book book2 = new Book
                {
                    Id = ObjectId.GenerateNewId(),
                    name = "Life"
                };
                collection.Save(book1);
                collection.Save(book2);
    
                var query = Query<Book>.EQ(u => u.Id, new ObjectId("5a5ee6360222da8ad498f3ff"));
                Book list = collection.FindOne(query);
                Console.WriteLine( "Book Name  " + list.name);
    
    
                Console.ReadLine();
            }
        }
        public class Book
        {
            [BsonId]
            public ObjectId Id { get; set; }
            public string name { get; set; }
    
            public Book()
            {
            }
    
            public Book(ObjectId id, string name)
            {
                this.Id = id;
                this.name = name;
            }
        }
    }
    

    【讨论】:

    【解决方案3】:

    它也因我们使用的 .Net 框架版本而异。如果我们使用 2x 驱动程序,它应该看起来像:

    var list = await collection.Find(new BsonDocument()).ToListAsync();
    

    方法二

    await collection.Find(new BsonDocument()).ForEachAsync(X=>Console.WriteLine(X));
    

    Reference Example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2017-02-11
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多