• MongoDB Find-5


    MongoDB Find和FindAsync参数不一样

     Find<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions options = null)

    FindSync<TDocument>(this IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions<TDocument, TDocument> options = null, 

    只有FindOptions<BsonDocument>支持Projection

    为了使用Projection ,需要使用FindSync

    示例

    // Requires official C# and .NET MongoDB Driver 2.5+
    using MongoDB.Bson;
    using MongoDB.Driver;
    using System;
    using System.Threading.Tasks;
    
    namespace MongoDBQuery
    {
        class Program
        {
            static async Task _Main()
            {
                
                IMongoClient client = new MongoClient("mongodb://host:port/");
                IMongoDatabase database = client.GetDatabase("hesuancore");
                IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("1149_yewushuju");
                
                // Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/
                
                BsonDocument filter = new BsonDocument();
    
                BsonDocument projection = new BsonDocument();
    
                projection.Add("_id", 1.0);
                
                var options = new FindOptions<BsonDocument>()
                {
                    Projection = projection
                };
                
                using (var cursor = await collection.FindAsync(filter, options))
                {
                    while (await cursor.MoveNextAsync())
                    {
                        var batch = cursor.Current;
                        foreach (BsonDocument document in batch)
                        {
                            Console.WriteLine(document.ToJson());
                        }
                    }
                }
            }
            
            static void Main(string[] args)
            {
                _Main().Wait();
            }
        }
    }
    

      

    1、建了一个小群:616945527(软件), 欢迎大家加入,加群口令abc123,硬件嵌入式开发者推荐75764412(单片机)。
    闲置域名www.nsxz.com出售(等宽等高字符四字域名,可组合多种有意义词语)。
  • 相关阅读:
    centos7下如何使用udev配置asm磁盘
    ORA-29786: SIHA attribute GET failed with error [Attribute 'SPFILE' sts[200]
    安装grid时报INS-40404错误
    clickhouse编译安装
    centos7通过rc.local文件添加自启动服务
    Error in invoking target 'agent nmhs' of makefile
    打补丁(18370031)
    2020 HFCTF
    2020省赛决赛
    2020西湖论剑
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/14304751.html
Copyright © 2020-2023  润新知