mongodbを使ってデータ構造を作るのに助けが必要です。 JavaScriptを使用してデータを操作しています。 私はこのタイプの文書構造を持っていたいと思いますが、私は正しいことをしていません。私はあなたがオブジェクトのコンテナとしても配列を使う傾向があり、mongodbの更新演算子がこの型の構造を必要としていることにも気付きました。MongoDB文書を整理してデータをより良く操作する
var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"),
items:[ {"name": "name", "other": "other"},
{"name": "name", "other": "other"},
{"name": "name", "other": "other"}
]}
が、好きではありません:)私は、ドキュメントをしたいと思いますどのようにこの方法でデータを整理する:例えば
{
"_id": {
"$oid": "dfdsdsfdsff54sdf5ds"
},
"displayName": "name",
"userId": "h8566d9482gghffhtry565",
"info": {
"level": 1,
"currentExperience": 0,
"requiredExperience": 0,
"missingExperience": 0
},
"statistics": {
"total": {
"stat1": 0,
"stat2": 0,
"stat3": 0,
"stat4": 0
},
"best": {
"distance": 0,
"stat1": 0,
"stat2": 0,
"stat3": 0,
"stat4": 0
},
"game": {
"one": 0,
"two": 0,
"three": 0,
"stat4": 0
}
},
"inventory": {
"item1": {
"property1": 0,
"property2": 0,
"property3": 0,
"property4": 0,
"level": {
"level": 1,
"currentExperience": 0,
"requiredExperience": 0,
"missingExperience": 0
},
"skins": [
"skin1",
"skin2",
"skin3"
]
},
"item2": {
"property1": 0,
"property2": 0,
"property3": 0,
"property4": 0,
"level": {
"level": 1,
"currentExperience": 0,
"requiredExperience": 0,
"missingExperience": 0
},
"skins": [
"skin1",
"skin2",
"skin3"
]
}
},
"notifications": {},
"rewards": {}
}
は、今私は問題があるイムかを説明します。 今、もし私がインベントリの中にある特定のアイテムのプロパティを変更しなければならない場合は、オブジェクト "inventory"を取る - >私が必要とするアイテムを探してプロパティを変更します。これの最後に 、更新演算子 "$ set"を使用して、 "inventory"フィールドを置き換えます。
データがほとんどなくても、このフィールドには何百もの「サブフィールド」があり、これは役に立たない無駄なリソースのようです。
残念ながら、$ inc演算子を使用すると、私は変更したいプロパティの「パス」を渡すことができません。 私を助けることができますか?ここ
は私が今
var userDoc = myCollection("userData");
var userData = userDoc.findOne({"userId": userId}, {items: 1, _id: 0});
//Other code
userData.inventory[itemName][propertyName] = //other code;
userDoc.update({"userId": userId},{"$set": userData});
おかげで、 よろしく