Java 如何避免Mongodb中重复的对象条目?

Java 如何避免Mongodb中重复的对象条目?,java,mongodb,playframework,Java,Mongodb,Playframework,现在在我的项目中,我正在使用Play framework(JAVA)和Mongodb。我的集合中有两个字段,分别是哈希和地址。请注意,地址字段是一个对象 我的收藏如下: db.hashed.find() { "_id" : ObjectId("55f04cd0d4c68ef1211e6ee4"), "hash" : "1axuhWcqKB", "address" : { "floor" : 0, "block" : "5", "city" : "Mumbai", "state" : "ABC",

现在在我的项目中,我正在使用Play framework(JAVA)和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" } } 
现在我在想,如何避免重复的地址条目,这显然不应该被插入到数据库中。我尝试在地址字段上执行ensureindex,但看起来它并不能解决这个问题

> db.users.ensureIndex({"address" : 1},{unique: true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
希望有人能帮我解决这个问题

我认为问题在于地址子对象中的“散列”属性。这三排都不同。这就是为什么mongodb不将它们视为重复密钥的原因。下面是我的工作示例(虽然没有播放框架,但它并没有改变这一点):


谢谢你的回复。那么这是否意味着我将永远无法在数据库级别检查重复项,看起来我只能通过代码来完成这项工作?也许你可以对此发表评论:)你可以通过将索引更改为:users.ensureIndex(“{address.floor:1,address.block:1,address.city:1,“+”address.state:1,address.pincode:1,address.latitude:1,“{name:\”unique\u address\”,unique:true}”)来检查重复项;你的意思是在数据库级别上类似这样的东西?db.ensureIndex(“{address.floor:1,address.block:1,address.city:1,“address.state:1,address.pincode:1,address.latitude:1”,“address.latitude:1}”,“{name:\“unique\U address\”,unique:true}”)。如果我遗漏了什么,请告诉我。顺便说一句,再次感谢您提供的信息性答案。在mongo命令行客户端中,只需使用以下命令:db.users.ensureIndex({“address.floor”:1,“address.block”:1,“address.city”:1,“address.pincode”:1,“address.latitude”:1,“address.longitude”:1},{name:“unique_address”,unique:true})
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}");