mongoengine中的复合主键
Composite primary key in mongoengine
我想在 mongodb 中设置复合主键以及默认主键 _id。我们如何使用 mongoengine 来实现?
db.collection.createIndex({_id: 1, otherVal: 1}, {unique: true});
如果您希望 _id
本身就是一个复合索引,您需要将 _id 定义为 Object
:
_id : {val : ObjectId("..."), other: ...}
Unique Compound Index
You can also enforce a unique constraint on compound indexes, as in the following prototype:
db.collection.createIndex( { a: 1, b: 1 }, { unique: true } )
These indexes enforce uniqueness for the combination of index keys and not for either key individually.
我想在 mongodb 中设置复合主键以及默认主键 _id。我们如何使用 mongoengine 来实现?
db.collection.createIndex({_id: 1, otherVal: 1}, {unique: true});
如果您希望 _id
本身就是一个复合索引,您需要将 _id 定义为 Object
:
_id : {val : ObjectId("..."), other: ...}
Unique Compound Index You can also enforce a unique constraint on compound indexes, as in the following prototype: db.collection.createIndex( { a: 1, b: 1 }, { unique: true } ) These indexes enforce uniqueness for the combination of index keys and not for either key individually.