On Thu, 16 Apr 2015, Trevor Oakley wrote:
> Here is the code (Java) I used which definitely did not work. It inserted
> countless urls all the same (RawUrl). What I expected was that the insert
> would automatically fail when a duplicate existed. That is how Cassandra
> worked when I tested it.
>
>
> BasicDBObject index = new BasicDBObject("RawUrl", 1).append("unique",
> true).append("dropDups", true);
You're setting:
append("dropDups", true);
Which means that duplicate entries silently get discarded, instead of an
error showing up.
>
> if (r.RawUrl != null) {
> BasicDBList urlList = mapRaw(r.RawUrls);
>
> BasicDBObject dbObj = new BasicDBObject("RawUrl",
> r.RawUrl).append("RawContent", r.RawContent).append("RawStatus",
> r.RawStatus);
> dbObj.append("RawUrls", new BasicDBObject("Urls", urlList));
> coll.save(dbObj);
> }
>
>
> coll.createIndex(index );
>
> Do I need to state the createIndex everytime or will Mongo detect the index?
You don't need to do createIndex more than one time. It is persisted in
the database. However, you do need to create the index *before* you
insert any documents. You now created it at the end, after inserting
URLs. And with the dropDups set, it will just then remove all duplicate
entries.
cheers,
Derick