Detailed analysis of GUID display issues in Mongodb

Detailed analysis of GUID display issues in Mongodb

Find the problem

I recently migrated the storage of a program to Mongodb and discovered a phenomenon: after the Guid type is written, the data read in the program is different from the data displayed by the client tool. A simple example is as follows:

 var doc = new {_id = 1, Value = Guid.Parse("{B63932BB-9688-432B-B3A0-C4AE3FD2696A}") };
 collection.InsertOne(doc.ToBsonDocument());

After writing to the DB, we view it from the client tool:

By comparison, we can find that the displayed value is different from the written form, but the read value is correct. The answer to this question can be found in the official documentation specification:

Official documentation: http://bsonspec.org/spec.html

Mongodb stores uuid as a binary type, and uses a subtype to mark its type as uuid. In the drivers provided for C#, Java, and Python, the subtype is mapped to 3 (Legacy UUID) by default, which is also displayed in the client tool:

In fact, most client tools can also support displaying the Legacy UUID as we wrote it.

Workaround

Although this does not affect the functionality of the program, it often affects our debugging of the program because the string we see at first glance is not in the format in which it was written. A better solution is to store in standard UUID format by default. You only need to set a global configuration:

 BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;

This is much more convenient:

Reference articles:

MongoDB Best Practices for UUID Data

Summarize

The above is the detailed content about the display problem of GUID in Mongodb. For more information about the display problem of GUID in Mongodb, please pay attention to other related articles on 123WORDPRESS.COM!

<<:  Examples of correct use of maps in WeChat mini programs

>>:  Summary of 9 excellent code comparison tools recommended under Linux

Recommend

Several ways to solve CSS style conflicts (summary)

1. Refine the selector By using combinators, the ...

Detailed tutorial on building Gitlab server on CentOS8.1

There is no need to say much about the difference...

Summary of Common Mistakes in Web Design

In the process of designing a web page, designers...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

How to solve the problem of forgetting the root password of Mysql on Mac

I haven't used mysql on my computer for a lon...

Detailed explanation of the JavaScript timer principle

Table of contents 1. setTimeout() timer 2. Stop t...

Vue3.0 implements the encapsulation of the drop-down menu

Vue3.0 has been out for a while, and it is necess...

When MySQL is upgraded to 5.7, WordPress reports error 1067 when importing data

I recently upgraded MySQL to 5.7, and WordPress r...

Detailed tutorial on installing Docker and docker-compose suite on Windows

Table of contents Introduction Download and insta...

Summary of principles for writing HTML pages for emails

Since HTML email is not an independent HOST page o...

Data Structure - Tree (III): Multi-way Search Tree B-tree, B+ tree

Multi-way search tree Height of a complete binary...

Apply provide and inject to refresh Vue page method

Table of contents Method 1: Call the function dir...

Solve the problem of HTML automatic formatting after saving in vscode

The version of vsCode has been updated in recent ...