2

I'm using Azure Cosmos DB with MongoDB Api with my project, developed with Golang and MongoDB Go Drivers. When I try to get a count using below code gives the error Invalid response from server, value field is not a number

itemCount, err := myCollection.CountDocuments(ctx, bson.D{{"$and", bson.A{bson.M{"userid": user.ID, "siteid": site.ID}}}})

The same code returns the number of items (in this case 532) when I tried it against a MongoDB database. I then decided to try the same query by writing a simple .Net application, which worked fine with both MongoDB and CosmosDB. Below is the code I used, and it printed 532 for both databases.

var count = database.GetCollection<Model>("myCollection").CountDocuments(filter);
Console.WriteLine(count);

I also tried querying both databases via Robo3T. The query db.getCollection('Consents').count({}) returned 532 from MongoDB while the same query returned NumberLong(532) from CosmosDB.

I wrote this behavior to MSDN forums, but no one relied. I'm not even sure if this is an issue of the database api or the driver. In case of driver, it works with MongoDB and it's what it promised me to do. In case of database api, it works with .Net drivers, and it tells me that Microsoft is well aware of this behavior and finds it acceptable and handles it within its drivers. Anyone have an idea how to overcome this situation?

Serdar Kalaycı
  • 391
  • 1
  • 4
  • 10
  • CosmosDB is not MongoDB, you shouldn't have the expectation that it should work due to compatibility mismatch. Having said that which version of MongoDB Go driver and MongoDB .NET driver are you using ? – Wan B. Jul 10 '19 at 01:49
  • I'm aware that CosmosDB is not 100% compatible with MongoDB, but the inconsistency between GO drivers and .Net drivers, being both developed by MongoDB and one handling the non-standard return of CosmosDB and the other does not made me wonder. BTW I'm using MongoDB Go Driver v1.0.3 – Serdar Kalaycı Jul 12 '19 at 11:02
  • Is there more error stack trace from the Go driver ? I couldn't find the error line that you posted in the driver itself. Also what do you mean by `Microsoft is well aware of this behavior and finds it acceptable and handles it within its drivers` ? Are you using Microsoft .NET drivers ? – Wan B. Jul 12 '19 at 22:43

1 Answers1

5

your filter should be:

itemCount, err := myCollection.CountDocuments(ctx, bson.M{"userid": user.ID, "siteid": site.ID})

bson.M will perform an implicit $and

owlwalks
  • 1,541
  • 13
  • 16
  • Thanks, this helped me to simplify the code but even without a filter two databases give different responses to the query and that seems to be the problem. – Serdar Kalaycı Jul 12 '19 at 10:58