2016-12-24 14 views
2

Morphia EntityクラスのArrayListの継承された@Embedded参照を使用しています。morphiaのArrayListから特定の要素を削除するにはどうすればよいですか?

@Entity 
public class First { 
    @Embedded private List<Second> secondClass; 
    private String title; 
    private Long id; 
    ... getter and setter .. methods 
} 

@Embedded 
public class Second { 
    @Embedded private List<Third> thirdClass; 
    private String titleSecond; 
    ... getter and setter .. methods 
} 

@Embedded 
public class Third { 
    private String titleThird; 
    private String logoUrl 
    ... getter and setter .. methods 
} 

JSON

{ 
    "secondClass": [{ 
    "thirdClass": [{ 
     "titleThird": "Java", 
     "logoUrl": "http://www.artsfon.com/pic/201510//artsfon.com-72885.jpg", 
    }, { 
     "titleThird": "ios", 
     "logoUrl": "http://www.artsfon.com/pic//1920x1080/artsfon.com-72885.jpg", 
    }], 
    "titleSecond": "Developer" 
    }], 
    "title": "Software Developer", 
    "id" : 1234567890 
} 

titleSecondidの与えられた値を持つ要素を削除する方法の質問

1)?

2)指定された値titleThirdidの要素を削除するにはどうすればよいですか?

は、最初のために私は次の実装しようとした:

UpdateOperations<First> ops; 
Query<First> updateQuery = datastore.createQuery(First.class).filter("id", Long.parseLong(id.trim())); 

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass.titleSecond", "Developer"); 
datastore.update(updateQuery, ops); 

をマッピングエラーを投げています:

Write failed with error code 16837 and error message 'cannot use the part (secondClass of secondClass.titleSecond) to traverse the element. 

私は質問1と2のためのアクションを実行できますか?どんな助けにも感謝します。

おそらくリンク質問:このようなIn Morphia how can i update one embedded Object inside an ArrayList

+0

最初にこのドキュメントをどのように作成しましたか?レコードを第3のクラスにプッシュできましたか?別の質問。第二のクラスはどのように第三のクラスに結びついていますか?第3クラスを第2クラスと同じレベルに移動できますか? – Veeram

+0

*最初にこの文書をどのように作成したのか不思議です*私は上記の 'JSON'オブジェクトを渡して' datastore.save(firstObject) 'で保存しています。 *レコードを第3クラスにプッシュできました*:はい、レコードを「MongoDB」で見ることができます。 *第2クラスは第3クラスにどのように結びついていますか*:私は 'Second Class'で' @Embedded private List thirdClass; 'を使用しました。投稿されたJSON( '" thirdClass ":[{...}]')をチェックしてください。あなたはすべての質問をクリアしてもらえますか? –

+0

実際には初めての節約ではなく、私の質問は既存のオブジェクトの3番目のクラスに新しいエントリを追加したいのですが、それは試したものですか? – Veeram

答えて

1

作成されたレコード。

db.myCollection.insertMany([{ 
     "_id" : ObjectId("58609ed483ce6037ec33fc8c"), 
     "secondClass" : [ 
       { 
         "thirdClass" : [ 
           { 
             "titleThird" : "Java", 
             "logoUrl" : "http://www.artsfon.com/pic" 
           }, 
           { 
             "titleThird" : "ios", 
             "logoUrl" : "http://www.artsfon.com/pic" 
           } 
         ], 
         "titleSecond" : "Developer" 
       } 
     ], 
     "title" : "Software Developer" 
}]) 

マイファーストクラス。残りのクラスは同じです。

@Entity("myCollection") 
public class First { 

    @Id 
    private ObjectId id; 
    private String title; 
    @Embedded 
    private List<Second> secondClass; 

最初の部分への回答。

UpdateOperations<First> ops; 
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c")); 

Second second = new Second(); 
second.setTitleSecond("Developer"); 
ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass", second); 
datastore.update(updateQuery, ops); 

第1部の代替ソリューション。

UpdateOperations<First> ops; 
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c")); 

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass", new BasicDBObject("titleSecond", "Developer")); 
datastore.update(updateQuery, ops); 

回答は2番目の部分です。

UpdateOperations<First> ops; 
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c")).field("secondClass.thirdClass.titleThird").equal("ios"); 

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass.$.thirdClass", new BasicDBObject("titleThird", "ios")); 
datastore.update(updateQuery, ops); 
+0

私は試みましたが、何も起こっていませんでした。例外なく成功した応答を得ました。私はデータベースをチェックしましたが、エントリはそこにありました。 'TitleSecond'を見つけてそれを取り出して' removeAll( "secondClass"、second'?)に渡すか、あなたのコードをコピーするだけですか? –

+0

完全なコードを追加しました。私はモルフィア1.3.0を使用しています – Veeram

+0

おかげでsagar、 "最初の部分の代替ソリューション"と "2番目の部分への回答"というタイトルは私のために働いています:) –

関連する問題