当来自mongodb的反向数据时,它会给出tiimeout异常

时间:2016-12-06 13:39:42

标签: asp.net mongodb

贝娄是我的代码

public async void TestLoops(long device,double date1,double date2)         {

        var  connectionString = "mongodb://localhost.:27017";//Connection string 
        MongoClientSettings settings1 = MongoClientSettings.FromUrl(new MongoUrl(connectionString));// Set Url to the MongoClient
        //MongoS

      // MongoClient mongoClient = new MongoClient(settings1);

        var client = new MongoClient(new MongoClientSettings
        {
            Server = new MongoServerAddress("connectionString"),
            ClusterConfigurator = builder =>
            {
                builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(1000)));
            }
        });


       //Server s = new MongoServerAddress(connectionString);
        var db = client.GetDatabase("tracking");//Specifing the Database name
        var user = db.GetCollection<BsonDocument>("location");//Specifing t
        var builder1 = Builders<BsonDocument>.Filter;
       // var builder1 = Builders<BsonDocument>.Filter;
       //int 
        var filt = builder1.Eq("device", device)  & builder1.Gte("timestamp", date1) & builder1.Lte("timestamp", date2);
        //var filter = builder1.Eq("device", 358740050124519);

        var data =  user.Find(filt).Count();
        lblmsg.Text = data.ToString();




    }

当我写var data = user.Find(filt);它工作正常

1 个答案:

答案 0 :(得分:0)

你可以做到

var collection = database.GetCollection<Type>("DBName");
var cursor = collection.Find(Query.EQ("FieldToMatch" : "ValueToMatch"));

var count = cursor.Count(); 

另一种方法

 db.collection.CountAsync(); // you can pass new BsonDocument() or a condition to countAsync based on your requirement.