[MongoDB]: Unique Index
MongoDB allows you to specify a unique constraint on an index. These constraints prevent applications from inserting documents that have duplicate values for the inserted fields.
> db.dbversity.createIndex( { “dbversity.post_id” : 1 } , { unique : true } )
{
“createdCollectionAutomatically” : true,
“numIndexesBefore” : 1,
“numIndexesAfter” : 2,
“ok” : 1
}
>
> db.dbversity.insert( { dbversity : { post_id : 1, post : “New post from dbversity”, comments : ” the most awaiting post” }, subscribers : 220, feeds : “new feeds”, likes : 200, post_id : 1 })
WriteResult({ “nInserted” : 1 })
>
> db.dbversity.find().pretty()
{
“_id” : ObjectId(“5527a38c9689e4b8db19b60d”),
“dbversity” : {
“post_id” : 1,
“post” : “New post from dbversity”,
“comments” : ” the most awaiting post”
},
“subscribers” : 220,
“feeds” : “new feeds”,
“likes” : 200,
“post_id” : 1
}
>
>
> db.dbversity.insert( { dbversity : { post_id : 1, post : “New post from dbversity”, comments : ” the most awaiting post” }, subscribers : 220, feeds : “new feeds”, likes : 200, post_id : 1 })
WriteResult({
“nInserted” : 0,
“writeError” : {
“code” : 11000,
“errmsg” : “insertDocument :: caused by :: 11000 E11000 duplicate key error index: dbversity.dbversity.$dbversity.post_id_1 dup key: { : 1.0 }”
}
})
>
>
> db.dbversity.insert( { dbversity : { post_id : 2} })
WriteResult({ “nInserted” : 1 })
>
>
> db.dbversity.find()
{ “_id” : ObjectId(“5527a38c9689e4b8db19b60d”), “dbversity” : { “post_id” : 1, “post” : “New post from dbversity”, “comments” : ” the most awaiting post” }, “subscribers” : 220, “feeds” : “new feeds”, “likes” : 200, “post_id” : 1 }
{ “_id” : ObjectId(“5527a3d29689e4b8db19b60f”), “dbversity” : { “post_id” : 2 } }
>
>
MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index.
Now, let’s see this … let’s insert duplicate data for comment_id field and create a unique index on it.
> db.dbversity.insert( { dbversity : { comment_id : 10} })
WriteResult({ “nInserted” : 1 })
> db.dbversity.insert( { dbversity : { comment_id : 10} }) // Insert failed since we’re inserting duplicate data entry for dbversity.post_id field ( duplicate entry with null value)
WriteResult({
“nInserted” : 0,
“writeError” : {
“code” : 11000,
“errmsg” : “insertDocument :: caused by :: 11000 E11000 duplicate key error index: dbversity.dbversity.$dbversity.post_id_1 dup key: { : null }”
}
})
>
Note : If a document does not have a value for a field, the index entry for that item will be null in any index that includes it.
>
db.dbversity.find()
{ “_id” : ObjectId(“5527a38c9689e4b8db19b60d”), “dbversity” : { “post_id” : 1, “post” : “New post from dbversity”, “comments” : ” the most awaiting post” }, “subscribers” : 220, “feeds” : “new feeds”, “likes” : 200, “post_id” : 1 }
{ “_id” : ObjectId(“5527a3d29689e4b8db19b60f”), “dbversity” : { “post_id” : 2 } }
{ “_id” : ObjectId(“5527a46e9689e4b8db19b610”), “dbversity” : { “comment_id” : 10 } }
>
>
> db.dbversity.find({ “dbversity.comment_id” :10}, { “dbversity.post_id” :1, _id :0})
{ “dbversity” : { } }
>
> db.dbversity.insert( { dbversity : { comment_id : 10, post_id : 3} })
WriteResult({ “nInserted” : 1 })
>
> db.dbversity.find()
{ “_id” : ObjectId(“5527a38c9689e4b8db19b60d”), “dbversity” : { “post_id” : 1, “post” : “New post from dbversity”, “comments” : ” the most awaiting post” }, “subscribers” : 220, “feeds” : “new feeds”, “likes” : 200, “post_id” : 1 }
{ “_id” : ObjectId(“5527a3d29689e4b8db19b60f”), “dbversity” : { “post_id” : 2 } }
{ “_id” : ObjectId(“5527a46e9689e4b8db19b610”), “dbversity” : { “comment_id” : 10 } }
{ “_id” : ObjectId(“5527a4969689e4b8db19b612”), “dbversity” : { “comment_id” : 10, “post_id” : 3 } }
>
> db.dbversity.find({ “dbversity.comment_id” :10}, { “dbversity.post_id” :1, _id :0})
{ “dbversity” : { } }
{ “dbversity” : { “post_id” : 3 } }
>
>
>
>
> db.dbversity.createIndex( { “dbversity.comment_id” : 1 } , { unique : true } )
{
“createdCollectionAutomatically” : false,
“numIndexesBefore” : 2,
“ok” : 0,
“errmsg” : “E11000 duplicate key error index: dbversity.dbversity.$dbversity.comment_id_1 dup key: { : null }”,
“code” : 11000
}
>
>
>
Note: Unique Index and Missing Field
If a document does not have a value for a field, the index entry for that item will be null in any index that includes it. Thus, in many situations you will want to combine the unique constraint with the sparse option. Sparse indexes skip over any document that is missing the indexed field, rather than storing null for the index entry. Since unique indexes cannot have duplicate values for a field, without the sparse option, MongoDB will reject the second document and all subsequent documents without the indexed field. Consider the following prototype.
db.collection.createIndex( { a: 1 }, { unique: true, sparse: true } )