Right now in my project, I'm using Play framework(JAVA) and Mongodb. There are two fields in my collection, which are hash and address. Please be informed that the address field is an object .
My collection looks like this :
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" } }
Now I'm thinking on, how to avoid the duplicate address entries, which obviously shouldn't get inserted in the database. I tried doing ensureindex over address field, but looks like it's not something that will solve this problem.
> db.users.ensureIndex({"address" : 1},{unique: true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
Hope someone can help me out on this.
I think the problem is in "hash" properties inside address sub-objects. They are different for all three rows. That's why mongodb doesn't treat them as duplicate keys. Here is my working example (wothout Play framework though but it doesn't change the point):
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
}
}
[Update] In case you need to check uniqueness of all fields of address except "hash" you need to change your index into something like:
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}");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments