Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CosmosDB UUID issue #7

Closed
deus42 opened this issue May 18, 2018 · 8 comments
Closed

CosmosDB UUID issue #7

deus42 opened this issue May 18, 2018 · 8 comments

Comments

@deus42
Copy link

deus42 commented May 18, 2018

Hi,

I'm currently facing issue with UUID 04 representation in CosmosDB mongo API driver:

"Message": "GuidRepresentation Standard is only valid with subType UuidStandard, not with subType UuidLegacy.\r\nИмя параметра: guidRepresentation"

They said they added it to all regions: https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/20206180-support-uuid-subtype-4

I just wonder how to make it work both MongoDB and CosmosDB with Standard UUID type?

Seems to me that only CSharpLegacy type will work for both. Any suggestions?

@alexandre-spieser
Copy link
Owner

alexandre-spieser commented May 20, 2018

Hello,

The IMongoDbContext of the repository exposes the following method:

        /// <summary>
        /// Sets the Guid representation of the MongoDb Driver.
        /// </summary>
        /// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
        void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation);

Allowing you set the guidRepresentation to whatever is needed for your application.

If you don't want to modify the global setting BsonDefaults.GuidRepresentation, you can specify the setting when you create your collection:

 IMongoDatabase db = ???;
 string collectionName = ???;
 var collectionSettings = new MongoCollectionSettings {
   GuidRepresentation = GuidRepresentation.Standard
 };
var collection = MongoDbContext.Database.GetCollection<BsonDocument>(collectionName, collectionSettings);

Then any GUIDs written to the collection will be in the standard format.

Note that when you read records from the database, you will get a System.FormatException if the GUID format in the database is different from the format in your collection settings.

@deus42
Copy link
Author

deus42 commented May 23, 2018

Yes, it is a bug in CosmosDB. I got rid of your repository (because of the static constructor) and use CSharpLegacy type. Otherwise, it won't work. I encourage you to remove the static constructor.

@alexandre-spieser
Copy link
Owner

alexandre-spieser commented May 24, 2018

Thanks for the feedback, I'll look into removing the static constructor.
I have not tested this, but are you sure you cannot overwrite the GuidRepresentation in the constructor of your repository though?

    public class TestRepository : BaseMongoRepository, ITestRepository
    {
        public TestRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
        {
              MongoDbContext.SetGuidRepresentation(MongoDB.Bson.GuidRepresentation.CSharpLegacy);
        }
    }

According to https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/MongoDefaults.cs, it should not cause issues:

        /// <summary>
        /// Gets or sets the representation to use for Guids (this is an alias for BsonDefaults.GuidRepresentation).
        /// </summary>
        public static GuidRepresentation GuidRepresentation
        {
            get { return BsonDefaults.GuidRepresentation; }
            set { BsonDefaults.GuidRepresentation = value; }
        }

@deus42
Copy link
Author

deus42 commented May 24, 2018

It is not causing an issue with MongoDB, but rather with CosmosDB with mongo wire protocol.

@StephenStrickland
Copy link

StephenStrickland commented Sep 27, 2018

Using MongoDbContext.SetGuidRepresentation() fixes the issue with Cosmos DB

@alexandre-spieser
Copy link
Owner

yeah it does. I've also removed the static constructor in the last version of the repo ;)

@jasonhartsell
Copy link

I'm still having trouble with this and can't seem to get the document records to show up in Azure Cosmos DB. Could you please elaborate on how the override is used and where it needs to be instantiated? Also, do I need to make some changes to the Startup or anything around the Document to associate to the user?

@pfekrati
Copy link

pfekrati commented Oct 6, 2022

I also can't get the docs to show up in CosmosDB. I'm getting this error: "Error querying documents: The GuidRepresentation for the reader is CSharpLegacy, which requires the binary sub type to be UuidLegacy, not UuidStandard"

I tried below code, but it's not working:

public class MongoDbRepository : BaseMongoRepository, IDocumentDbRepository
{
    public MongoDbRepository(string connectionString, string databaseName) : base(connectionString, databaseName)
    {
        MongoDbContext.SetGuidRepresentation(MongoDB.Bson.GuidRepresentation.CSharpLegacy);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants