我试图基于两个字段创建一个唯一的多键索引:
我的文件:
> db.users.find().pretty()
{
"_class" : "bean.User",
"_id" : ObjectId("52f3945e2f9d426e9dcb1ac8"),
"commandes" : [
{
"idCommande" : 1,
"nom" : "toto",
"prenom" : "TATA",
"adresse" : "",
"ville" : "Brest",
"codePostal" : "29200",
"montantTotal" : 0,
"voyagesSouscrits" : [
{
"idVoyage" : "123",
"duree" : 10,
"nbPersonnes" : 0,
"villeDepart" : "Nantes",
"prixVoyage" : 100000
}
]
}
],
"mail" : "toto@toto.fr",
"password" : "1234"
}
我已经确保我的邮件有一个唯一的索引。现在我想为每个“idCommande”创建一个唯一的约束,那么就不可能在我的用户的“命令”中创建一个具有相同“idCommande”的“命令”。
我试过了
db.users.ensureIndex({_id : 1, idCommande : 1 },{unique :true});
并且 :
db.users.ensureIndex({_id : 1, idCommande : 1 },{unique :true, multikey : true});
但我仍然可以插入相同的“idCommande”。
我知道 addToSet 存在,但我不想限制我的全局文档
你有什么想法吗?
提前致谢