2017-03-19 3 views
0

私は配列内のただ1つの整数の値を変更する方法を探しています。 配列全体を取得したくない場合は、別のスレッドが整数の値を変更できるため、値を変更して元に戻してください。Java MongoDbは配列の値を変更します

[ 
    { 
    "row 1": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ], 
    "row 2": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ], 
    "row 3": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ], 
    "row 4": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ], 
    "row 5": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ], 
    "row 6": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ], 
    "row 7": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ], 
    "row 8": [ 
     0, 
     0, 
     0, 
     0, 
     0 
    ] 
    } 
] 

これは「showInstance」文書の中にある「席」文書で、「showInstance」文書は「ショー」ドキュメント内にあります。

これは、要素の位置を知っている場合、あなたは、例えば、値を更新するためにそれを使用することができますショー文書

{ 
    "_id": 1, 
    "name": "Great rock show", 
    "description": "The party will roll all night long during the North American WorldWired tour as we’re psyched to announce that award-winning DJ and producer Mix Master Mike will be spinning some of your favorite (and ours!) hard rock and metal tracks throughout the evening at all of the stadium dates this summer. A pioneer in the hip-hop and DJ communities, you’ll be able to catch his skillful, hard-hitting scratch work once the doors open each night and between acts before we hit the stage.", 
    "band_id": 3, 
    "instances": [ 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7db" 
     }, 
     "date": { 
     "$date": "2017-04-07T15:00:00.000Z" 
     }, 
     "price": 140, 
     "theater_id": 3, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 5": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 6": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 7": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 8": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    }, 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7dc" 
     }, 
     "date": { 
     "$date": "2017-05-04T15:00:00.000Z" 
     }, 
     "price": 758, 
     "theater_id": 5, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 5": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 6": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 7": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 8": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 9": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 10": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    }, 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7dd" 
     }, 
     "date": { 
     "$date": "2017-05-23T15:00:00.000Z" 
     }, 
     "price": 634, 
     "theater_id": 9, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    }, 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7de" 
     }, 
     "date": { 
     "$date": "2017-06-02T15:00:00.000Z" 
     }, 
     "price": 398, 
     "theater_id": 3, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 5": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 6": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 7": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 8": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    }, 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7df" 
     }, 
     "date": { 
     "$date": "2017-06-29T15:00:00.000Z" 
     }, 
     "price": 36, 
     "theater_id": 9, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    }, 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7e0" 
     }, 
     "date": { 
     "$date": "2017-08-08T15:00:00.000Z" 
     }, 
     "price": 377, 
     "theater_id": 11, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 5": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 6": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 7": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 8": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 9": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 10": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    }, 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7e1" 
     }, 
     "date": { 
     "$date": "2017-08-28T15:00:00.000Z" 
     }, 
     "price": 885, 
     "theater_id": 12, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 5": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 6": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 7": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 8": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 9": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 10": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 11": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 12": [ 
      0, 
      0, 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    }, 
    { 
     "_id": { 
     "$oid": "58cea4779f3489310471b7e2" 
     }, 
     "date": { 
     "$date": "2017-09-26T15:00:00.000Z" 
     }, 
     "price": 853, 
     "theater_id": 3, 
     "seats": [ 
     { 
      "row 1": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 2": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 3": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 4": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 5": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 6": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 7": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ], 
      "row 8": [ 
      0, 
      0, 
      0, 
      0, 
      0 
      ] 
     } 
     ] 
    } 
    ], 
    } 
    } 
} 
+0

'seat '配列の' row'配列要素全体を更新していますか? – Veeram

+0

いいえ、行の中の1つの要素を更新するだけです – Yuval

+0

'' seat'配列を特定した後に更新する ''行 'とその' '行の要素を決定する基準は何ですか? – Veeram

答えて

0

です

BasicDBObject setQuery = new BasicDBObject("$set", new BasicDBObject("instances.$.seats.0.row 1.0", 1)); 
collection.update(someSearchQuery, setQuery); 

更新

ここクエリおよび更新するための例です:

DBCollection collection = db.getCollection("shows"); 
BasicDBObject query = new BasicDBObject("_id", 1); 
query.put("instances._id", new ObjectId("58cea4779f3489310471b7db")); 

BasicDBObject setQuery = new BasicDBObject("$set", new BasicDBObject("instances.$.seats.0.row 1.0", 1)); 
collection.update(query, setQuery); 

Mongoのクエリ

db.seats.update({"_id":1,"instances._id": ObjectId("58cea4779f3489310471b7db")}, {$set : {"instances.$.seats.0.row 1.0" : 100}}) 

上記クエリが "ロー1" を更新する必要があります要素を100に、私はjava exapleも更新しました。

+0

こんにちはDarshan、ありがとう。 私はMongoDBのにちょっと新しいだと私は完全なクエリを得ることができれば、それは素晴らしい:) コレクション名=「ショー」になります ショーのid = 1 showInstanceId =「58cea4779f3489310471b7db」 とshowInstance insindeという配列があります Tnx – Yuval

+0

複数の「showInstance」が複数存在する可能性はありますか? –

+0

はい、ショーは複数のインスタンスを持つことができ、それぞれのインスタンスは独自のシートマトリックスを持っています – Yuval

関連する問題