1
votes

I am using Azure cosmos dB Emulator to do CRUD operations on MongoDB using MongoDB C# Drivers. I am able to create DB and collection using C# in emulator. This is my sample code to create DB and Collection..

IMongoDatabase db = dbClient.GetDatabase("<My DB name>");
db.CreateCollection("<Collection Name>");

These queries are working fine but when I am trying to insert sample data into this collection its throwing below error

Command insert failed: Unknown server error occurred when processing this request..

My sample code to insert sample data is

IMongoCollection<UserProfile> collection = db.GetCollection<UserProfile("<Collection Name>");
            UserProfile c = new UserProfile();
            c.ID = 21;
            c.UserName = "<Some Name> ";
            c.Email = "<Email ID>";
            collection.InsertOne(c);

How to use MongoDB C# Drivers to do CRUD operations in Azure cosmos dB Emulator And how to run mongo queries in Emulator instead of SQL queries? Thanks in Advance

1

1 Answers

1
votes

The UI for MongoDB API in Emulator is not yet implemented (it's coming though), but everything else should work. There are two tutorials you need to combine for your use case:

https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator (look for MongoDB section there)

https://docs.microsoft.com/en-us/azure/cosmos-db/create-mongodb-dotnet - build, run and make sure it works new connection string for emulator and then just inject your code, it will work.