0

I have an application that is using Mongo DB currently. I am looking to move the app to Azure and trying to use Cosmos DB. I upgraded the C# Mongo DB Driver in the code to the latest version 2.7.0 and it is working all fine still using Mongo DB.

I then used the Cosmos DB Migration Tool to migrate Data to the Azure Cosmos DB Emulator and changed the connection string in my web config to point to the emulator. The Application is loading and some reference data is getting returned on my first screen but my GetById query below is not working?

    public virtual T GetById(TKey id)
    {
        if (typeof(T).IsSubclassOf(typeof(EntityBase)))
        {
            return GetById(new ObjectId(id as string));
        }

        //code removed for brevity
    }

    public virtual T GetById(ObjectId id)
    {
        var filter = Builders<T>.Filter.Eq("_id", id);

        var result = collection.FindSync<T>(filter).FirstOrDefault();

        return result;
    }

The result when I connect to my Mongo DB in web config is the single entity by the Object Id - however when I change the connection string to the emulator nothing is returned?

This is how the object looks in MongoDB (visualized using RoboMongo)

{
    "_id" : ObjectId("5b97a56b6381fecd00f0e10a"),
    "LastUpdatedOn" : [ 
        NumberLong(636722473812102569), 
        -240
    ],
    "CreatedOn" : [ 
        NumberLong(636722473396922518), 
        -240
    ],
    "LastUpdatedBy" : "SYSTEM",
    "CreatedBy" : "TestUser",
    "VersionNumber" : 3,
    "Name" : "Audi",

This is how the same object looks in the Azure Cosmos DB Emulator after the migration using the migration Data Tool

{
    "_id": "5b97a56b6381fecd00f0e10a",
    "LastUpdatedOn": [
        636722473812102500,
        -240
    ],
    "CreatedOn": [
        636722473396922500,
        -240
    ],
    "LastUpdatedBy": "SYSTEM",
    "CreatedBy": "TestUser",
    "VersionNumber": 3,
    "Name": "Audi",

Could the reason it is not working be that that Id had lost the Object("")? I tried to update the Azure Cosmos DB collection to add that but it was giving an error saying value expected as if I wasn't specifying correct JSON format.

8
  • Have you tried adding the _id in the filter as a string rather than an ObjectId? Commented Sep 18, 2018 at 11:36
  • @NickChapsas - I havent no - I will give that a go - just was hoping to make as little code changes as possible Commented Sep 18, 2018 at 11:39
  • I would expect it to work without that change tbh but the emulator document representation implies otherwise. Commented Sep 18, 2018 at 11:41
  • Try doing a "Reset Data" (right click on the Emulator tray icon) Commented Sep 18, 2018 at 11:43
  • @NickChapsas - that worked :) Commented Sep 18, 2018 at 11:43

2 Answers 2

1

Judging from the CosmosDB emulator representation of the document it looks like you need to change your GetById method to use a string instead of an ObjectId.

Something like this should work:

public virtual T GetById(TKey id)
{
    if (typeof(T).IsSubclassOf(typeof(EntityBase)))
    {
        return GetById(id as string);
    }

    //code removed for brevity
}

public virtual T GetById(string id)
{
    var filter = Builders<T>.Filter.Eq("_id", id);

    var result = collection.FindSync<T>(filter).FirstOrDefault();

    return result;
}
Sign up to request clarification or add additional context in comments.

1 Comment

cheers - it has worked for retrieving the data to screen however throws a new error when trying to update that data - but ill create a new question for that if I cant get it resolved
0

The actual reason this was not working was using the Azure Cosmos DB Migration Too is designed for use with the Cosmos DB SQL API. I wanted to target the MongoDB API for Cosmos DB API.

The way to get the data into the Emulator for this was to use the mongoimport and mongoexport exe's as detailed here:

https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb-migrate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.