将.NET Guid转换为MongoDB ObjectID [英] Convert .NET Guid to MongoDB ObjectID

查看:298
本文介绍了将.NET Guid转换为MongoDB ObjectID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将.NET GUID转换为MongoDB ObjectID(在C#中).另外,我可以再次将其从ObjectID转换回相同的GUID吗?

How can I convert a .NET GUID to a MongoDB ObjectID (in C#). Also, can I convert it back again to the same GUID from the ObjectID?

推荐答案

您不能将ObjectId转换为GUID,反之亦然,因为它们是两种不同的东西(不同的大小,算法).

You can't convert ObjectId into GUID and vice versa, because they are two different things(different sizes, algoritms).

您可以对mongoDb _id使用任何类型,包括GUID.

You can use any type for mongoDb _id including GUID.

例如,在官方C#驱动程序中,您应该指定属性[BsonId]:

For example in official c# driver you should specify attribute [BsonId]:

[BsonId]
public Guid Id {get;set;}

[BsonId]
public int Id {get;set;}

ObjectId :

BSON ObjectID是一个12字节的值 由4个字节的时间戳组成 (自纪元以来的秒数),一个3字节 机器ID,2字节的进程ID和一个 3字节计数器.请注意 时间戳记和计数器字段必须为 存储的大字节序不像其余的 BSON.这是因为它们是 逐字节比较,我们想要 确保订单数量大增.

A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine id, a 2-byte process id, and a 3-byte counter. Note that the timestamp and counter fields must be stored big endian unlike the rest of BSON. This is because they are compared byte-by-byte and we want to ensure a mostly increasing order.

GUID :

GUID的值表示为 一个32个字符的十六进制字符串, 如 {21EC2020-3AEA-1069-A2DD-08002B30309D}, 通常存储为128位 整数

The value of a GUID is represented as a 32-character hexadecimal string, such as {21EC2020-3AEA-1069-A2DD-08002B30309D}, and is usually stored as a 128-bit integer

这篇关于将.NET Guid转换为MongoDB ObjectID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆