MongoDB和Go – 使用动态数据模型

huangapple go评论94阅读模式
英文:

Mongo DB and Go - using dynamic data models

问题

我面临一个问题,不确定应该选择哪条路。所以我在这里问一下。我有一个应用程序,其中可以有产品,也可以有产品的元数据。这些元数据可以在前端创建和删除。假设今天每个产品有两个元数据(例如名称、价格),那么明天可能是三个、四个或更多,甚至少于两个。所以这是动态的。我试图将数据表示如下:

产品 = 
{
    "_id": mongo
    "名称": 字符串
    "描述": 字符串
    "基础价格": 数字
    "创建者": 用户 mongo _id
    "创建时间": 时间戳
    "修改时间": 时间戳
    "元数据": BSON 对象(包含来自 ProductMetadata 集合的所有键和它们的值。例如:{"类别": "餐具","材料": "橡木、铜","长度": 5.6})

}

产品元数据 = 
{
    "_id": mongo
    "名称": 字符串(例如 - "类别" 或 "材料" 或 "高度")
    "类型": 字符串(指示它可以保存的值的类型,如字符串/整数/数组。例如 - "字符串")
    "创建时间": 时间戳
    "修改时间": 时间戳
}

如你所见,这是一个纯动态的情况,所以在代码层面上表示模型的结构是不可能的。

我的问题是,我如何使用 mgo 和 Go 语言来实现这样的功能?如果我需要使用反射,请有人指点我一个好的博客/教程,我可以在那里获取更多信息。或者如果你认为在建模数据的方法上存在根本性问题,请纠正我,以便可以轻松地使用 Go 实现。

在 Python 中,实现这个不是问题。我知道这一点。但是我对 Go 的实现感到困惑。

请帮忙。

提前感谢。

英文:

I am faced with a problem where I am not sure which path to take. So I am asking it here. I have an application where there can be products and there can be metadata for a product. These metadata can be created and deleted from the front end. So let's say, today a each product has two meta data (e.g. Name, Price) then tomorrow it can be three, four or more or even less than two. So this is dynamic. I am trying to represent the data as follows

Product = 
{
	"_id": mongo
	"Name": string
	"Description": string
	"BasePrice": number
	"CreatedBy": user mongo _id
	"CreatedAt": timestamp
	"ModifiedAt": timestamp
	"MetaData": BSON object (having all the keys from ProductMetadata collection and their values. e.g. {"Category": "table ware", "Material": "oak wood, copper", "Length": 5.6})

}

ProductMetadata = 
{
	"_id": mongo
	"Name": string (e.g. - "Category" or "Material" or "Height")
	"Type": string (indicating what kind of value it can hold like string/integer/array. e.g. - "string")
	"CreatedAt": timestamp
	"ModifiedAt": timestamp
}

As you can see this is a pure dynamic situation so having a structure at the code level which represent the model is impossible.

My problem is, how do I implement such a thing using mgo and Go lang? If I need to use reflection then can some one please point me towards a good blog/tutorial where I can get a little bit more info. Or if you think that there is a fundamental problem in the approach of modeling the data then please correct me so that it can be easily implemented using Go.

In Python, it would not be a problem to implement this. I know that. But I am confused about the Go implementation.

Please help.

Thanks in advance

答案1

得分: 3

如果元数据的键是唯一的,为什么不直接使用一个映射(map)呢?

也就是说,你的Product结构体看起来像这样:

struct Product {
    ID        bson.ObjectId `bson:"_id,omitempty"`
    Name string
    Description string
     ... 省略其他字段 ...
    MetaData map[string]map[string]interface{} // 字符串到字符串到任意类型的映射
}

如果你可以有多个相同元数据的实例,比如2个类别,可以使用一个列表(list):

struct Product {
    ID        bson.ObjectId `bson:"_id,omitempty"`
    Name string
    Description string
     ... 省略其他字段 ...
    MetaData []map[string]interface{} // 字符串到任意类型的映射的列表
}
英文:

If Keys for metadata are unique, why not just use a map.

meaning your Product struct looks like:

struct Product {
    ID        bson.ObjectId `bson:"_id,omitempty"`
    Name string
    Description string
     ... omitted other fields ...
    MetaData map[string]map[string]interface{} // map of string -> map of string -> anything
}

If you can have more than one instance of a given meta data, ie: 2 categories, use a list:

struct Product {
    ID        bson.ObjectId `bson:"_id,omitempty"`
    Name string
    Description string
     ... omitted other fields ...
    MetaData []map[string]interface{} // list of maps of string -> anything
}

huangapple
  • 本文由 发表于 2016年1月26日 01:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/34998644.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定