2013-07-03 6 views
5
//get the collection 
DBCollection coll = MongoDBClient.getInstance().getAlarmInfoCollection(); 

DBObject query = new BasicDBObject(); 
query.put(aa, "51d2b09f81b8a943f9e825aa"); 

DBObject update = new BasicDBObject(); 
DBObject history = new BasicDBObject(); 
history.put("ishistory", 1); 
history.put("acknowledged", 1); 
history.put("state", 1);   
update.put("$set", history); 
coll.updateMulti(query, update); 

mongodbでドキュメントを更新する前にプログラムを使用しています。 (フィールド「州」は更新されません)、propramはエラーを報告しませんでした。私のプログラムに間違いはありますか?MongoDBは、同時に3つのフィールドを更新するためにjavaを使用します。時には2つのフィールドのみが更新されます。

+0

"aa"の値は何ですか? – Trisha

+0

'update.multi()'ではなく 'coll.update(query、update)'をチェックしてください。 – tostao

+0

しかし、クエリが複数のレコードと一致する場合は、一致するすべての値を更新するためにこの更新プログラムが必要であると仮定します。 – Trisha

答えて

8

コードの動作を示すユニットテストを作成しました。 updateMultiが一致するすべての文書 を更新します

  • (すなわち 複数のフィールドが$ setキーワードで更新することができます)あなたが一度に複数のフィールドを更新することができるはず

    1. :このユニットテストは、ことを証明しています

      @Test 
      public void shouldUpdateAllMatchingFieldsUsingMultiUpdate() throws UnknownHostException { 
          MongoClient mongoClient = new MongoClient(); 
          DB db = mongoClient.getDB("myDatabase"); 
          DBCollection coll = db.getCollection("coll"); 
          coll.drop(); 
      
          //Put some test data in the database 
          for (int i = 0; i < 5; i++) { 
           DBObject value = new BasicDBObject(); 
           value.put("fieldToQuery", "a"); 
           value.put("ishistory", 2+i); 
           value.put("acknowledged", 3+i); 
           value.put("state", 14+i); 
           value.put("someOtherArbitraryField", Math.random() * 1000); 
           System.out.println(value); 
           coll.insert(value); 
          } 
      
          DBObject query = new BasicDBObject("fieldToQuery", "a"); 
      
          DBObject history = new BasicDBObject().append("ishistory", 1) 
                    .append("acknowledged", 1) 
                    .append("state", 1); 
      
          DBObject update = new BasicDBObject("$set", history); 
      
          //This syntax for update means that all three fields will be set to the new given value 
          Assert.assertEquals(update.toString(), "{ \"$set\" : { \"ishistory\" : 1 , \"acknowledged\" : 1 , \"state\" : 1}}"); 
      
          //Do the update, updating every document that matches the query 
          coll.updateMulti(query, update); 
      
          //find The new values 
          DBCursor updatedDocuments = coll.find(query); 
          for (DBObject updatedDocument : updatedDocuments) { 
           Assert.assertEquals(updatedDocument.get("ishistory"), 1); 
           Assert.assertEquals(updatedDocument.get("acknowledged"), 1); 
           Assert.assertEquals(updatedDocument.get("state"), 1); 
           System.out.println(updatedDocument); 
          } 
      } 
      

      このテスト(すべてのJavaのMongoDBが、これはTestNGのないJUnitのテストを使用していますが、それはこのケースではかなり似ているように、注意してください)渡します。例えば、実行のために、データベース内のデータは次のとおりです。

      { "fieldToQuery" : "a" , "ishistory" : 2 , "acknowledged" : 3 , "state" : 14 , "someOtherArbitraryField" : 700.7831275035031} 
      { "fieldToQuery" : "a" , "ishistory" : 3 , "acknowledged" : 4 , "state" : 15 , "someOtherArbitraryField" : 72.65538582882736} 
      { "fieldToQuery" : "a" , "ishistory" : 4 , "acknowledged" : 5 , "state" : 16 , "someOtherArbitraryField" : 980.0065367659304} 
      { "fieldToQuery" : "a" , "ishistory" : 5 , "acknowledged" : 6 , "state" : 17 , "someOtherArbitraryField" : 91.58266286854722} 
      { "fieldToQuery" : "a" , "ishistory" : 6 , "acknowledged" : 7 , "state" : 18 , "someOtherArbitraryField" : 448.19176202797115} 
      

      テストの終了時に、updateMultiは$集合演算子を使って呼び出された後に、データベース内の文書は、次のとおりです。

      { "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 700.7831275035031} 
      { "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 72.65538582882736} 
      { "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 980.0065367659304} 
      { "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 91.58266286854722} 
      { "fieldToQuery" : "a" , "ishistory" : 1 , "acknowledged" : 1 , "state" : 1 , "someOtherArbitraryField" : 448.19176202797115} 
      

      したがって、更新は機能し、一致するすべてのドキュメントに対して3つのフィールドを1に設定し、ドキュメント上の他のデータには触れませんでした。

      それは、元の質問のコードと同じことをやっているべきであるが、問合せ、更新や歴史を設定するための私の構文は、もう少し短く読みやすく、ビットであることは注目に値するかもしれない:

      DBObject query = new BasicDBObject("fieldToQuery", "a"); 
      
      DBObject history = new BasicDBObject().append("ishistory", 1) 
                   .append("acknowledged", 1) 
                   .append("state", 1); 
      
      DBObject update = new BasicDBObject("$set", history); 
      

      queryに一致するすべてのレコードを指定した値で更新することを前提としていますか?したがって、あなたはupdateMultiを使用していますか?

  • +0

    正解ではなく、最後の値のみを設定します。 – tostao

    +0

    最後の値のみを設定しますか? updateMulti?それとも$セット? – Trisha

    +0

    あなたの回答は「状態」のみ更新されます。 – tostao

    関連する問題