强迫mongodb使用uuid而不是objectid

发布于 2025-02-10 17:10:43 字数 806 浏览 1 评论 0原文

我想使用纯JSON-DATA(在服务中不序列化/序列化)存储在MongoDB中,然后回信并提供纯JSON。 我唯一需要的是,MongoDB应该将“ _id”作为uuid而不是objectid创建。

如果我有以下JSON:

[ { 'name': 'Data1' },
  { 'name': 'Data2' } ]

并使用C#MongoDB.Driver 2.16.0插入MongoDB,我将获得以下JSON:

[{ "_id" : ObjectId("62b5765ab43274529aa913c1"), "name" : "Data1" }, { "_id" : ObjectId("62b5765ab43274529aa913c2"), "name" : "Data2" }]

MongoDB使用Objectid类型。
我如何强制使用mongoDB使用uuid而不是objectid?

在这里,代码插入记录:

var testColl = db.GetCollection<BsonDocument>("test", new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Unspecified });
var testJson = "[{'name':'Data1'},{'name':'Data2'}]";
testColl.InsertMany(BsonSerializer.Deserialize<BsonDocument[]>(testJson));

I want to use pure json-data (without de-/serializing in the service) to store in MongoDB and read back and deliver pure json.
The only think I would need is, MongoDB should create "_id" as UUID instead of ObjectId.

If I have the following json:

[ { 'name': 'Data1' },
  { 'name': 'Data2' } ]

and insert this to MongoDB with the c# MongoDB.Driver 2.16.0 I get the following json back:

[{ "_id" : ObjectId("62b5765ab43274529aa913c1"), "name" : "Data1" }, { "_id" : ObjectId("62b5765ab43274529aa913c2"), "name" : "Data2" }]

where MongoDb is using the ObjectId type.
How do I force MongoDB to use UUID instead of ObjectId?

Here the code to insert the records:

var testColl = db.GetCollection<BsonDocument>("test", new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Unspecified });
var testJson = "[{'name':'Data1'},{'name':'Data2'}]";
testColl.InsertMany(BsonSerializer.Deserialize<BsonDocument[]>(testJson));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2025-02-17 17:10:43

我可以想象这个简单的解决方法:

    static void Main(string[] args)
    {
        var client = new MongoClient();
        var db = client.GetDatabase("db");
        var testColl = db.GetCollection<BsonDocument>("coll", new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Unspecified});
        var testJson = "[{'name':'Data1'},{'name':'Data2'}]";

        var res = BsonSerializer.Deserialize<BsonDocument[]>(testJson);
        testColl.InsertMany(res.Select(d => new BsonDocumentWithId(d).ToBsonDocument()));
    }

    public class BsonDocumentWithId : IConvertibleToBsonDocument
    {
        public BsonDocumentWithId(BsonDocument doc) => Document = doc.DeepClone().AsBsonDocument;

        public BsonDocument Document { get; }

        public BsonDocument ToBsonDocument() => Document.Add("_id", new BsonBinaryData(Guid.NewGuid().ToByteArray(), BsonBinarySubType.UuidStandard));
    }

I can imagine this simple workaround though:

    static void Main(string[] args)
    {
        var client = new MongoClient();
        var db = client.GetDatabase("db");
        var testColl = db.GetCollection<BsonDocument>("coll", new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Unspecified});
        var testJson = "[{'name':'Data1'},{'name':'Data2'}]";

        var res = BsonSerializer.Deserialize<BsonDocument[]>(testJson);
        testColl.InsertMany(res.Select(d => new BsonDocumentWithId(d).ToBsonDocument()));
    }

    public class BsonDocumentWithId : IConvertibleToBsonDocument
    {
        public BsonDocumentWithId(BsonDocument doc) => Document = doc.DeepClone().AsBsonDocument;

        public BsonDocument Document { get; }

        public BsonDocument ToBsonDocument() => Document.Add("_id", new BsonBinaryData(Guid.NewGuid().ToByteArray(), BsonBinarySubType.UuidStandard));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文