MongoDB Composite Key
Asked Answered
N

4

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/

Norenenorfleet answered 28/8, 2011 at 16:7 Comment(1)
i know the post is too old but: mongo is not a relational DB so morphia is not an ORM :)Conciliator
P
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/

Parshall answered 29/8, 2011 at 3:0 Comment(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?Norma
@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.aParshall
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).Norma
@GiovanniBotta I think the approach you mentioned at the end has trouble using the _id index, did you check that?Brune
@Brune Not sure what you mean. Unfortunately I am not working with Mongo anymore so I haven't look at this in a while.Norma
@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"Brune
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.Apropos
M
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.

Maryammaryann answered 28/8, 2011 at 22:6 Comment(0)
W
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;

Wendling answered 12/11, 2012 at 19:58 Comment(4)
Hi, @Nic Cottrell What is 'coll' in coll.find ?Rufina
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.Rufina
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...Wendling
@NewbieSoftwareEngineer I'll need more information about the error before I could help you further.Wendling
T
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)
Tabloid answered 2/3, 2021 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.