2013-04-01 17 views
0

狀態。如果一個函數調用java中做過這樣:如何知道的MongoDB的功能在Java中

collection.update(match, new BasicDBObject("$pull", update)); 

我怎麼能知道這條語句執行後更新數據庫中的文檔數量?

我還有一個問題是,我想從一個陣列刪除文檔中的元素,如果該元素存在的集合。我該如何輕鬆做到這一點?我是否應該查詢它是否存在於集合中,然後刪除它或者還有其他更簡單的方法嗎?

回答

0

1)DBCollection.html#update方法返回一個包含一Ñ屬性一個寫結果對象,which contains the number of documents affected in the write operation.

2)我認爲沒有辦法做,在一個單一的操作。如果在插入時,元素已經在其他文檔的數組上,則可以執行更新以從數組中刪除該元素。類似的東西:

DBObject newElement = ... 

DBObject query = BasicDBObjectBuilder.start() 
    .add("array", newElement) 
    .get(); 

DBObject update = BasicDBObjectBuilder.start() 
    .push("$pull") 
     .add("array", newElement) 
    .pop() 
    .get(); 

collection.update(query, update); 
collection.insert(newElement);