【问题标题】:Store a list of Guids in MongoDb as a list of strings在 MongoDb 中将 Guid 列表存储为字符串列表
【发布时间】:2014-10-29 22:43:50
【问题描述】:

我想将 Guid 列表存储为 MongoDB 中的字符串列表。对于单个 Guid,我使用以下属性:

[BsonRepresentation(BsonType.String)]
public Guid GuidId{ get; set; }

是否有一个属性可以做同样的事情:

public List<Guid> GuidIdList { get; set; }

我发现了如何使用约定来做到这一点,但我真的很喜欢用属性来做到这一点:

How can I tell the MongoDB C# driver to store all Guids in string format?

【问题讨论】:

  • 将 GUID 存储为字符串的一个缺点是不能使用 mongodb 聚合框架stackoverflow.com/a/11952062
  • @chridam 字符串从来都不是这样。您链接到的是描述 2.6 之前二进制值的限制。

标签: c# mongodb


【解决方案1】:

在工作人员的建议下,我通过一个小测试尝试了这个:

[BsonRepresentation(BsonType.String)]
public List<Guid> GuidIds { get; set; }

你猜怎么着,这行得通!这是您在 MongoDB 中看到的:

{
  "_id" : ObjectId("5450fa00f0640335e4117d46"),
  "GuidId" : "f9e22da8-97c5-4dc7-ab26-ce5e095427a4",
  "GuidIds" : ["48e1d0a9-74a6-4b51-af6b-b217012adeac", "5e1751b1-d945-4baa-beed-847e9696caa9"]
}

所以该属性对列表中的元素起作用。我认为它会对列表本身进行字符串表示。


额外信息,没有属性

public List<Guid> GuidIds { get; set; }

你在 MongoDB 中得到这个:

{
  "_id" : ObjectId("5450fc4bf0640327b82cb259"),
  "GuidId" : "e76f37a1-eba6-4afe-8ee4-8d431f97978c",
  "GuidIds" : [new BinData(3, "E4x6bMtj606H06snHwmVGw=="), new BinData(3, "uV9vktKLzkOF/ylcOckfYg==")]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 2015-10-06
    • 2021-09-24
    相关资源
    最近更新 更多