MongoDB Find

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, 

 

为了使用Project ,需要使用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();
        }
    }
}

  

MongoDB Find

上一篇:windows下mysqldump定时备份


下一篇:java.sql.SQLException Parameter index out of range (0 1 ).