2015-09-09 83 views
1

現在在我的項目中,我使用了Play框架(JAVA)和Mongodb。我的收藏中有兩個字段,分別是哈希和地址。請注意,地址欄是一個對象。如何避免Mongodb中的重複對象條目?

我的收藏是這樣的:

db.hashed.find() 
{ "_id" : ObjectId("55f04cd0d4c68ef1211e6ee4"), "hash" : "1axuhWcqKB", "address" : { "floor" : 0, "block" : "5", "city" : "Mumbai", "state" : "ABC", "pincode" : 0, "latitude" : "77.6876", "longitude" : "13.57558", "hash" : "1axuhWcqKB" } } 
{ "_id" : ObjectId("55f04cd1d4c68ef1211e6ee6"), "hash" : "1ay4P407qF", "address" : { "floor" : 0, "block" : "5", "city" : "Mumbai", "state" : "ABC", "pincode" : 0, "latitude" : "77.6876", "longitude" : "13.57558", "hash" : "1ay4P407qF" } } 
{ "_id" : ObjectId("55f04cd2d4c68ef1211e6ee8"), "hash" : "1ayDn3Zemh", "address" : { "floor" : 0, "block" : "5", "city" : "Mumbai", "state" : "ABC", "pincode" : 0, "latitude" : "77.6876", "longitude" : "13.57558", "hash" : "1ayDn3Zemh" } } 

現在我思考,如何避免重複的地址條目,這顯然不應該獲得插入到數據庫中。我試圖通過地址字段確保索引,但看起來並不能解決這個問題。

> db.users.ensureIndex({"address" : 1},{unique: true}) 
{ 
"createdCollectionAutomatically" : false, 
"numIndexesBefore" : 2, 
"numIndexesAfter" : 2, 
"note" : "all indexes already exist", 
"ok" : 1 
} 

希望有人能幫助我解決這個問題。

回答

1

我認爲問題出在地址子對象內的「散列」屬性。它們對於所有三行都不相同。這就是爲什麼mongodb不會將它們視爲重複鍵的原因。這是我的工作示例(wothout播放框架雖然,但它不會改變點):

import java.util.LinkedHashMap; 
import java.util.Map; 

import org.jongo.Jongo; 
import org.jongo.MongoCollection; 

import com.mongodb.DB; 
import com.mongodb.MongoClient; 

public class MongodbTest { 
    public static void main(String[] args) throws Exception { 
     String tableName = "UniqueAddressTest"; 
     DB mdb = new MongoClient("localhost:27017").getDB(tableName); 
     Jongo jdb = new Jongo(mdb); 
     MongoCollection users = jdb.getCollection(tableName); 
     users.drop(); 
     users.ensureIndex("{address:1}", "{unique:true}"); 
     Map<String, Object> user1 = new LinkedHashMap<String, Object>(); 
     Map<String, Object> address1 = new LinkedHashMap<String, Object>(); 
     address1.put("floor", 0); 
     address1.put("block", "5"); 
     address1.put("city", "Mumbai"); 
     address1.put("state", "ABC"); 
     address1.put("pincode", 0); 
     address1.put("latitude", "77.6876"); 
     address1.put("longitude", "13.57558"); 
     address1.put("hash", "1axuhWcqKB"); 
     user1.put("address", address1); 
     users.insert(user1); 
     users.insert(user1); // Here is where duplicate key exception happens 
    } 
} 

[更新]如果您需要檢查地址的所有領域的獨特性,除了「哈希」你需要改變您的索引類似於:

users.ensureIndex("{address.floor:1, address.block:1, address.city:1, " + 
      "address.state:1, address.pincode:1, address.latitude:1, " + 
      "address.longitude:1}", "{name:\"unique_address\", unique:true}"); 
+0

感謝您的回覆。那麼這是否意味着我永遠無法在數據庫級別檢查重複項,看起來我只能通過代碼來執行此操作?也許你可以對此發表評論:) – murasing

+0

你可以通過改變你的索引來查看重複內容:users.ensureIndex(「{address.floor:1,address.block:1,address.city:1,」+ \t「address.state:1,address.pincode:1,address.latitude:1,」+ \t \t「address.longitude:1}」,「{name:\」unique_address \「,unique:true} 「); – rsutormin

+0

我會更好地更新我的答案...完成。 – rsutormin