36

I'm just getting started with MongoDb and I've noticed that I get a lot of duplicate records for entries that I meant to be unique. I would like to know how to use a composite key for my data and I'm looking for information on how to create them. Lastly, I am using Java to access mongo and morphia as my ORM layer so including those in your answers would be awesome.

Morphia: http://code.google.com/p/morphia/

1
  • 2
    i know the post is too old but: mongo is not a relational DB so morphia is not an ORM :)
    – MoienGK
    Mar 16, 2015 at 8:28

4 Answers 4

72

You can use objects for the _id field as well. The _id field is always unique. That way you kind of get a composite primary key:

 { _id : { a : 1, b: 1} }

Just be careful when creating these ids that the order of keys (a and b in the example) matters, if you swap them around, it is considered a different object.

The other possibility is to leave _id alone and create a unique compound index.

db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
//Deprecated since version 3.0.0, is now an alias for db.things.createIndex()

https://docs.mongodb.org/v3.0/reference/method/db.collection.ensureIndex/

7
  • I have a followup question: how would you query MongoDB to get all records for which 'a' is equal to some value and 'b' can be anything? Nov 20, 2013 at 21:24
  • @GiovanniBotta: That's a bit tricky. I forgot the details, but something like "_id > {a: X} and _id < {a: Y}" should work. You can also make a separate index on _id.a
    – Thilo
    Nov 22, 2013 at 10:46
  • 2
    actually I figured that out, just query for { "_id.x" : "something" } and it works for any of the fields in the key (although performance varies depending on the ordinality of it). Nov 22, 2013 at 14:23
  • 2
    @GiovanniBotta you mentioned you figured out you could use "_id.x" in the query. If you do that, then you can't take advantage of an index in the "_id" field (the default). Instead you can do like Thilo mentioned, so something like: { "_id" : { $gte: { a : 1 }, $lt: { a : 2 } } }. Syntax might be slightly off, but the point is you use a subdocument to compare against "_id" isntead of going directly against "_id.x"
    – eglasius
    May 20, 2014 at 7:12
  • 2
    Thank you so much for pointing out that composite _id keys field order matters! Due to your help, we've tracked down a bug we introduced in a MongoDB aggregation, where we grouped by fields in a different order than our Spring Data domain object had declared. Oct 18, 2017 at 19:32
4

You can create Unique Indexes on the fields of the document that you'd want to test uniqueness on. They can be composite as well (called compound key indexes in MongoDB land), as you can see from the documentation. Morphia does have a @Indexed annotation to support indexing at the field level. In addition with morphia you can define compound keys at the class level with the @Indexed annotation.

3

I just noticed that the question is marked as "java", so you'd want to do something like:

final BasicDBObject id = new BasicDBObject("a", aVal)
        .append("b", bVal)
        .append("c", cVal);
results = coll.find(new BasicDBObject("_id", id));

I use Morphia too, but have found (that while it works) it generates lots of errors as it tries to marshall the composite key. I use the above when querying to avoid these errors.

My original code (which also works):

final ProbId key = new ProbId(srcText, srcLang, destLang);
final QueryImpl<Probabilities> query = ds.createQuery(Probabilities.class)
  .field("id").equal(key);
Probabilities probs = (Probabilities) query.get();

My ProbId class is annotated as @Entity(noClassnameStored = true) and inside the Probabilities class, the id field is @Id ProbId id;

4
  • Hi, @Nic Cottrell What is 'coll' in coll.find ? Apr 11, 2017 at 9:23
  • Thanks, @Nic Cottrell, but your code is not running. It is showing errors. Before db.mycollection.find you had used coll.find and as per my knowledge, it was the variable name in which you had stored your collection. But now in your current code what is mycollection? And how am I suppose to declare this variable? One more thing, new BasicDBObject in results = db.myCollection.find(new BasicDBObjet("_id", id)); is also showing some error. I am a bit new in Java and MongoDB so maybe some of my questions might seem a bit dumb to you, I am apologizing for that in advance. :) Thanks again. Apr 12, 2017 at 4:51
  • 1
    Sorry - tried to answer you via my phone and broke my previous answer. You'll need to get an object representing the collection to do the find - will be of class com.mongodb.BasicDBObject... Apr 13, 2017 at 11:54
  • @NewbieSoftwareEngineer I'll need more information about the error before I could help you further. Apr 17, 2017 at 8:33
0

I will try to explain with an example:

  1. Create a table Music
  2. Add Artist as a primary key

Now since artist may have many songs we have to figure out a sort key. The combination of both will be a composite key.

Meaning, the Artist + SongTitle will be unique.

something like this:

{
"Artist" : {"s" : "David Bowie"},
"SongTitle" : {"s" : "changes"},
"AlbumTitle" : {"s" : "Hunky"},
"Genre" : {"s" : "Rock"},
}

Artist key above is: Partition Key SongTitle key above is: sort key

The combination of both is always unique or should be unique. Rest are attributes which may vary per record.

Once you have this data structure in place you can easily append and scan as per your custom queries.

Sample Mongo queries for reference:

db.products.insert(json file path)
db.collection.drop(json file path)
db.users.find(json file path)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.