JSON必须如何将MongoDB迫使Bindata子类型4

发布于 2025-02-09 23:36:59 字数 612 浏览 2 评论 0原文

我正在使用C#驱动程序mongodb.driver 2.16.0 我正在尝试插入纯JSON。 我希望_id用作新的UUID类型AKA Bindata subtype 4。但是我只能得到Subtype 3或纯字符串。

var testColl = db.GetCollection<BsonDocument>("test");
var testJson = "[{'_id':CSUUID('609e26f6-dbad-4dba-ba37-4e35f0e348ec'),'name':'Data1'},{'_id':CSUUID('609e26f6-dbad-4dba-ba37-4e35f0e348ff'),'name':'Data2'}]";
testColl.InsertMany(BsonSerializer.Deserialize<BsonDocument[]>(testJson));

我尝试了'_id':'609E26F6-DBAD-4DBA-BA37-4E35F0E348EC',导致字符串。
我尝试了'_id':uuid('609e26f6-dbad-4dba-ba37-4e35f0e348ec'),在Subtype 3中进行了混音。

示例代码还会导致

子类型3。

I'm using the c# driver MongoDB.Driver 2.16.0
I'm trying to insert pure json.
I want the _id to be used as the new UUID type aka BinData subtype 4. But I get only subtype 3 or pure string.

var testColl = db.GetCollection<BsonDocument>("test");
var testJson = "[{'_id':CSUUID('609e26f6-dbad-4dba-ba37-4e35f0e348ec'),'name':'Data1'},{'_id':CSUUID('609e26f6-dbad-4dba-ba37-4e35f0e348ff'),'name':'Data2'}]";
testColl.InsertMany(BsonSerializer.Deserialize<BsonDocument[]>(testJson));

I tried '_id':'609e26f6-dbad-4dba-ba37-4e35f0e348ec' which results in a string.
I tried '_id':UUID('609e26f6-dbad-4dba-ba37-4e35f0e348ec') which rusults in subtype 3.
And the example code also results in subtype 3.

How must the json look to make a subtype 4?

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

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

发布评论

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

评论(1

北座城市 2025-02-16 23:36:59

您有以下选项:

  1. 配置bsondefaults.guidrepresentationmode = guidrePresentationMode.v3;。在这种情况下,您将使用文档中指定的子类型(或通过属性在最后查看文档)。如果是csuuid - ,那么子类型为3,uuid -4。

  2. 配置guidserialization.unspecified。例如,在收集期间创建:

      db.getCollection&lt; bsondocument&gt;(
        “ coll”, 
        new Mongocollectionsettings(){guidRepresentation = guidRepresentation.unspecified});
     

这种情况类似于#1,我们从文档中获得了子类型,但仅适用于特定集合

  1. 配置guid> guidserialization.standard强迫使用此特定集合的子类型,无论subtype在文档中提供:

      var coll = db.getCollection&lt; bsondocument&gt;(“ coll”,new mongocollectionsettings(){guidrePresentation = guidrepresentation.standard});
    var testjson =“ [{'_id':csuuID('609e26f6-dbad-4dba-ba37-4e37-4e35f0e348ec'),'name':'data1'},{'_ id'} 4E35F0E348FF'),'名称':'data2'}]”;
    Coll.InsertMany(bsonserializer.deserialize&lt; bsondocument []&gt;(testjson)); //插入子类型为4(UUID/标准)
     

您还可以在全球范围内配置GuidRepresentation: bsondefaults.guidrepresentation = guidrepresentation.standard;。在这种情况下,您将默认情况下强制使用此表示形式(对于guidrepresentationmode.v2默认模式),

您可以找到详细信息在这里

You have the following options:

  1. Configure BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;. In this case, you will work with subTypes specified in the document (or via attributes see the doc in the end). If it's CSUUID - then subType is 3, UUID - 4.

  2. Configure GuidSerialization.Unspecified. For example during collection creating:

    db.GetCollection<BsonDocument>(
        "coll", 
        new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Unspecified});
    

this case is similar to #1 where we get used subType from a document, but only for a particular collection

  1. Configuring GuidSerialization.Standard to force using this particular subType for a collection regardless a subType provided in the document:

    var coll = db.GetCollection<BsonDocument>("coll", new MongoCollectionSettings() { GuidRepresentation = GuidRepresentation.Standard});
    var testJson = "[{'_id':CSUUID('609e26f6-dbad-4dba-ba37-4e35f0e348ec'),'name':'Data1'},{'_id':CSUUID('609e26f6-dbad-4dba-ba37-4e35f0e348ff'),'name':'Data2'}]";
    coll.InsertMany(BsonSerializer.Deserialize<BsonDocument[]>(testJson)); // inserted subType is 4 (UUID/Standard)
    

You also can configure GuidRepresentation globally like: BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;. In this case you will force using this representation by default everywhere (for GuidRepresentationMode.V2 which is default mode)

You can find details here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文