我遇到了一个问题,我不知道要采取哪条路径。所以我在这里问。我有一个应用程序,可以有产品,可以有产品的元数据。这些元数据可以从前端创建和删除。所以,假设今天每个产品都有两个元数据(例如Name,Price),那么明天它可以是三个,四个或更多,甚至不到两个。所以这是动态的。我想代表数据如下Mongo DB和Go - 使用动态数据模型
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
}
正如你可以看到这是如此具有代表模型是不可能的代码级的结构的纯动态情况。
我的问题是,如何使用mgo和Go lang来实现这样的事情?如果我需要使用反射,那么可以请一些人指向一个好的博客/教程,我可以获得更多信息。或者,如果您认为在建模数据的方法中存在根本性问题,那么请纠正我以便使用Go轻松实现。
在Python中,实现它不会有问题。我知道。但是我对Go的实现感到困惑。
请帮忙。
在此先感谢
会这样HTTP ://stackoverflow.com/questions/18340031/unstructured-mongodb-collections-with-mgo帮助? –
谢谢我认为这是我想要的:) – SRC