I am using meteor for development which using mongo db as store. Thats means that all operations are just display object value (frontend) or operate object (backend). For example, from [user] collection "copy" value (userId) to [message] collection but different key name. is there any way better to describe the relationship between two object rather than using
我正在使用meteor进行开发,使用mongo db作为商店。这意味着所有操作只是显示对象值(前端)或操作对象(后端)。例如,从[user]集合“copy”值(userId)到[message]集合但不同的键名称。有没有什么方法可以更好地描述两个对象之间的关系而不是使用
message.userId = user._id
maybe using a object to describe
也许用一个物体来描述
{"userId","_id"}
1 个解决方案
#1
0
There are many ways to skin a cat. This is how I would do it.
皮肤猫的方法有很多种。我就是这样做的。
I would first create a mapping object with key-value pairs which represent the field relations between the two objects. The keys are the keys from the first object and the values are the keys from the second object.
我首先创建一个具有键值对的映射对象,它们表示两个对象之间的字段关系。键是第一个对象的键,值是第二个对象的键。
{
"userId":"_id",
"userName":"name"
//...
}
Then I would use a function like this to apply the mapping object to two objects:
然后我会使用这样的函数将映射对象应用于两个对象:
function applyMapping(fromObj, toObj, mappingObj) {
for (fromKey in mappingObj) {
var toKey = mappingObj[fromKey];
toObj[toKey] = fromObj[fromKey];
}
}
#1
0
There are many ways to skin a cat. This is how I would do it.
皮肤猫的方法有很多种。我就是这样做的。
I would first create a mapping object with key-value pairs which represent the field relations between the two objects. The keys are the keys from the first object and the values are the keys from the second object.
我首先创建一个具有键值对的映射对象,它们表示两个对象之间的字段关系。键是第一个对象的键,值是第二个对象的键。
{
"userId":"_id",
"userName":"name"
//...
}
Then I would use a function like this to apply the mapping object to two objects:
然后我会使用这样的函数将映射对象应用于两个对象:
function applyMapping(fromObj, toObj, mappingObj) {
for (fromKey in mappingObj) {
var toKey = mappingObj[fromKey];
toObj[toKey] = fromObj[fromKey];
}
}