Get the App
SLTechnology News&Howtos  ›  Database  › 

How to solve the display problem of GUID in Mongodb

Shulou Source: shulou.com Published: 2022-06-01 09:07:03 11月12日 Update

This article mainly explains how to solve the display problem of GUID in Mongodb, the content is clear, interested friends can learn, I believe it will be helpful after reading.

Find a problem

Recently, I migrated the storage of a program to Mongodb, and found that after the Guid type is written, what is read in the program is different from that shown 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 the DB, we look at it from the client tool:

By comparison, it can be found that the displayed value is different from the written value, but there is no problem with the read value. The answer to this question can be found in the official documentation specification:

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

In Mongodb, uuid is stored as a binary type, and a subtype is used to mark its type as uuid. In the driver provided for Clover Magazine java Legacy UUID, its subtype is mapped to 3 (Legacy UUID) by default, which is also shown in the client tool:

In fact, most client-side tools also support the presentation of Legacy UUID as it was when we wrote it.

Solution method

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

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard

This makes it much more convenient:

After reading the above content, do you have a further understanding of how to solve the display problem of GUID in Mongodb? if you want to learn more, you are welcome to follow the industry information channel.

Tags: Problem customer client tool program type content storage Chinese customs official document method time format learning influence binary global interest function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei NVidia Docker OPPO Reno