0

i trying to create an unique multikey index based on two fields :

My document :

> 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"
}

I already ensure an unique index on my mail. And now i want to creat an unique constrain for each "idCommande", then it would not be possible to create an "commande" with same "idCommande" in my user's "commandes".

i tried an

db.users.ensureIndex({_id : 1, idCommande : 1 },{unique :true});

and also :

db.users.ensureIndex({_id : 1, idCommande : 1 },{unique :true, multikey : true});

but i stil can insert same "idCommande".

I know addToSet exists, but i don't want a constrain on my global document

Have you got any idea ?

Thanks in advance

Lombric
  • 830
  • 2
  • 11
  • 23

2 Answers2

0

I think

db.users.ensureIndex({_id: 1, "commandes.idCommande" : 1 },{unique :true})

should be enough

xlembouras
  • 8,215
  • 4
  • 33
  • 42
0

No the answer should be

db.users.ensureIndex({"commandes.idCommande" : 1 },{unique :true});

This will prevent duplicate entries for 'idCommande'.

vmr
  • 1,895
  • 13
  • 24
  • 1
    Well, the OP said it should not be globally unique. On the other hand, the OP also asks for `idDocument`, not for uniqueness of `idCommande`... – mnemosyn Feb 06 '14 at 15:00
  • 1
    ìdDocument`is his figment of imagination. All he wants is uniqueness for ìdCommande – vmr Feb 06 '14 at 15:05
  • Seems I'm not qualified for mind-reading. These aren't the droids I'm looking for. – mnemosyn Feb 06 '14 at 15:06
  • @user2323241 can u please confirm/decide what you want? – vmr Feb 06 '14 at 15:08
  • This is not correct because your result will do this : for a document 1, my idCommande will be 1,2,3,4 for example. and for a document 2 my idCommande will be 5,6,7. And I want 1,2,3,4,... for my document 2 ! – Lombric Feb 06 '14 at 18:29
  • 1
    Now I understand your question. And the simple answer to that is you CANT do this is in MongoDB. Indexes are meant to be across documents not within a document. You can add this validation before you insert docs to mongoDB. Hope this helps. – vmr Feb 06 '14 at 18:57