强迫mongodb使用uuid而不是objectid
我想使用纯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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以想象这个简单的解决方法:
I can imagine this simple workaround though: