简体   繁体   中英

MongoDB C# Drivers to do CRUD operations in Azure cosmos dB Emulator

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM