MongoDB 复合键

发布于 2024-12-01 13:42:41 字数 257 浏览 1 评论 0原文

我刚刚开始使用 MongoDb,我注意到我得到了很多重复的条目记录,而我本打算是唯一的。我想知道如何对我的数据使用复合键,并且我正在寻找有关如何创建它们的信息。最后,我使用 Java 来访问 mongo 和 morphia 作为我的 ORM 层,因此将它们包含在您的答案中会很棒。

吗啡:http://code.google.com/p/morphia/

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

只是在用心讲痛 2024-12-08 13:42:41

您也可以使用 _id 字段的对象。 _id 字段始终是唯一的。这样你就得到了一个复合主键:

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

创建这些 id 时要小心,键的顺序(示例中的 a 和 b)很重要,如果你交换它们,它会被视为不同的对象。

另一种可能性是单独保留 _id 并创建唯一的复合索引

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/参考/方法/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:

 { _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/

请你别敷衍 2024-12-08 13:42:41

您可以在您想要的文档字段上创建唯一索引想要测试唯一性。正如您从文档中看到的,它们也可以是复合的(在 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.

§对你不离不弃 2024-12-08 13:42:41

我只是注意到这个问题被标记为“java”,所以你想做类似的事情:

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

我也使用 Morphia,但发现(虽然它有效)它在尝试编组复合键时会生成很多错误。我在查询时使用上面的内容来避免这些错误。

我的原始代码(也有效):

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();

我的 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:

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;

久隐师 2024-12-08 13:42:41

我将尝试用一个例子来解释:

  1. 创建一个音乐
  2. 添加艺术家作为主键

现在,由于艺术家可能有很多歌曲,我们必须找出排序键
两者的组合将是一个复合键

这意味着,Artist + SongTitle 将是唯一的。

像这样的东西:

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

上面的Artist键是:Partition Key
上面的 SongTitle 键是: sort key

两者的组合总是唯一的或者应该是唯一的。其余属性可能因记录而异。

一旦您拥有此数据结构,您就可以根据您的自定义查询轻松附加和扫描。

Mongo 查询示例供参考:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文