Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the display problem of GUID in Mongodb

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 255

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report