Search code examples
c#azureazure-cosmosdbmongodb-.net-driver

CosmosDB with MongoDB API


Hi I am a newbie on MongoDB and CosmosDB and I try to this in c#

MongoClientSettings settings = MongoClientSettings.FromUrl(
                      new MongoUrl(connectionString)
                    );
                    settings.SslSettings =
                      new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };    
    var mongoClient = new MongoClient(settings);
    var mongoDatabase = mongoClient.GetDatabase("MYDATABASE");
    var mongoCollection = mongoDatabase.GetCollection<BsonDocument>("MYCOLLECTION");

    var builder = Builders<BsonDocument>.Filter;            
    var filter = builder.Lt("mac", "001BC50670101BB8") & builder.Gte("date", "2016-09-18T00:00:00Z") & builder.Gte("date", "2017-09-22T00:00:00Z");

    var query = mongoCollection.Find<BsonDocument>(filter).ToList<BsonDocument>();

But when it runs the query on the server I get this error:

Errors":["An invalid query has been specified with filters against path(s) that are not range-indexed. Consider adding allow scan header in the request."

I have found that I should add the "x-ms-documentdb-query-enable-scan" header to my request. But how I can do this?


Solution

  • Solution found in comment section:

    Query changed from

    builder.Lt("mac", "001BC50670101BB8")
    

    to

    builder.Eq("mac", "001BC50670101BB8")