How do I avoid duplicate object entry in Mongodb?

murasing

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.

rsutormin

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.

edited at
0

Comments

0 comments
Login to comment

Related

How do I send a message to the view that a duplicate entry exists?

How do I duplicate a Squarespace portfolio entry and retain the background image?

How to avoid duplicate object creation

How do I avoid saving duplicate data? [Django]

How do I best avoid inserting duplicate records in CakePHP?

How do I avoid printing duplicate numbers? [C language]

How to avoid adding duplicate objects to an array in MongoDB

How to Avoid Duplicate Entries in MongoDb Meteor App

How to avoid duplicate using Mongodb c++

how do i search for an entry in mongodb database using java in netbeans?

how to restrict entry of duplicate object in core data

How do I remove the duplicate Key check before insert in mongoDB?

How do I convert String to an Object in MongoDB?

How to avoid duplicate data entry in a table in Room Db?

How do i bind an "Entry Cell" to a custom object using Xamarin?

How do I add an entry to a custom object array

Using Spring Data,Mongodb, how can I avoid Duplicate vertices error

In Swift, how do I avoid both optionals and nil object references?

How do I avoid getting a object with nested values here?

Mongodb avoid duplicate entries

How can I remove a duplicate startup entry?

How can I avoid duplicate items in a checkListBox

How can I avoid duplicate templates in Meteor?

How I can avoid duplicate records for insert

How can I avoid duplicate switch statements

how do i change duplicate entry (unique value date) error into other message such as the date already booked

Avoid a duplicate value when I update array using $push on MongoDB

How do I update a table using a staging table to avoid inserting duplicate records?

How do I structure/design a multipage dash application to avoid duplicate callback errors?