MongoDB 复合键
我刚刚开始使用 MongoDb,我注意到我得到了很多重复的条目记录,而我本打算是唯一的。我想知道如何对我的数据使用复合键,并且我正在寻找有关如何创建它们的信息。最后,我使用 Java 来访问 mongo 和 morphia 作为我的 ORM 层,因此将它们包含在您的答案中会很棒。
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/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您也可以使用 _id 字段的对象。 _id 字段始终是唯一的。这样你就得到了一个复合主键:
创建这些 id 时要小心,键的顺序(示例中的 a 和 b)很重要,如果你交换它们,它会被视为不同的对象。
另一种可能性是单独保留 _id 并创建唯一的复合索引。
https://docs.mongodb.org/v3.0/参考/方法/db.collection.ensureIndex/
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:
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.
https://docs.mongodb.org/v3.0/reference/method/db.collection.ensureIndex/
您可以在您想要的文档字段上创建唯一索引想要测试唯一性。正如您从文档中看到的,它们也可以是复合的(在 MongoDB 中称为复合键索引)。 Morphia 确实有一个 @Indexed 注释来支持字段级别的索引。除了使用吗啡之外,您还可以使用
@Indexed
注释在类级别定义复合键。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.我只是注意到这个问题被标记为“java”,所以你想做类似的事情:
我也使用 Morphia,但发现(虽然它有效)它在尝试编组复合键时会生成很多错误。我在查询时使用上面的内容来避免这些错误。
我的原始代码(也有效):
我的 ProbId 类注释为
@Entity(noClassnameStored = true)
,在 Probabilities 类中,id 字段为@Id ProbId id;
>I just noticed that the question is marked as "java", so you'd want to do something like:
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):
My ProbId class is annotated as
@Entity(noClassnameStored = true)
and inside the Probabilities class, the id field is@Id ProbId id;
我将尝试用一个例子来解释:
表
音乐艺术家
作为主键
现在,由于艺术家可能有很多歌曲,我们必须找出
排序键
。两者的组合将是一个
复合键
。这意味着,
Artist
+SongTitle
将是唯一的。像这样的东西:
上面的
Artist
键是:Partition Key
上面的
SongTitle
键是:sort key
两者的组合总是唯一的或者应该是唯一的。其余属性可能因记录而异。
一旦您拥有此数据结构,您就可以根据您的自定义查询轻松附加和扫描。
Mongo 查询示例供参考:
I will try to explain with an example:
table
MusicArtist
as aprimary 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
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: