[mongodb-user] enforcing unique indexes in the shell

609 views
Skip to first unread message

Wes

unread,
Apr 29, 2010, 7:19:49 PM4/29/10
to mongodb-user
I am trying to prepare a jira for an issue encountered through java
api, but I can not replicate it in the shell.

What needs to be done to force unique indexes to be enforced here:



> db.blog.save({
... _id : "1",
... blog_name : "My First Blog",
... entries : [
... {
... entry_id : "123",
... entry_name : "My first entry",
... posted : "2010-01-01",
... comments : [ { user : "bob", comment : "cool"}, { user :
"joe", comment : "not cool" } ]
... },
... {
... entry_id : "124",
... entry_name : "My second entry",
... posted : "2010-01-01",
... comments : [ { user : "jane", comment : "not cool"},
{ user : "sam", comment : "not cool" } ]
... },
... {
... entry_id : "125",
... entry_name : "My third entry",
... posted : "2010-01-02",
... comments : [ { user : "jane", comment : "not cool"},
{ user : "sam", comment : "not cool" } ]
... }
... ]
... } )
> db.blog.find()
{ "_id" : "1", "blog_name" : "My First Blog", "entries" : [
{
"entry_id" : "123",
"entry_name" : "My first entry",
"posted" : "2010-01-01",
"comments" : [
{
"user" : "bob",
"comment" : "cool"
},
{
"user" : "joe",
"comment" : "not cool"
}
]
},
{
"entry_id" : "124",
"entry_name" : "My second entry",
"posted" : "2010-01-01",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
},
{
"entry_id" : "125",
"entry_name" : "My third entry",
"posted" : "2010-01-02",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
}
] }
> db.blog.ensureIndex({"entries.entry_id": 1}, {unique: true})
> db.blog.ensureIndex({"entries.comments.user": 1}, {unique: true})
> db.blog.reIndex()
{
"nIndexesWas" : 3,
"msg" : "indexes dropped for collection",
"ok" : 1,
"nIndexes" : 3,
"indexes" : [
{
"name" : "_id_",
"ns" : "dt.blog",
"key" : {
"_id" : 1
}
},
{
"_id" : ObjectId("4bd9f8f93c981d3d878fca20"),
"ns" : "dt.blog",
"key" : {
"entries.entry_id" : 1
},
"name" : "entries.entry_id_1",
"unique" : true
},
{
"_id" : ObjectId("4bd9f90c3c981d3d878fca21"),
"ns" : "dt.blog",
"key" : {
"entries.comments.user" : 1
},
"name" : "entries.comments.user_1",
"unique" : true
}
],
"ok" : 1
}
> db.blog.find()
{ "_id" : "1", "blog_name" : "My First Blog", "entries" : [
{
"entry_id" : "123",
"entry_name" : "My first entry",
"posted" : "2010-01-01",
"comments" : [
{
"user" : "bob",
"comment" : "cool"
},
{
"user" : "joe",
"comment" : "not cool"
}
]
},
{
"entry_id" : "124",
"entry_name" : "My second entry",
"posted" : "2010-01-01",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
},
{
"entry_id" : "125",
"entry_name" : "My third entry",
"posted" : "2010-01-02",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
}
] }
> db.blog.update({ blog_name: "My First Blog", "entries.entry_id" : "123"}, { $addToSet : {'entries.$.comments' : { 'user' : 'sam', 'comment' : 'this should get insetted' } } })
> db.blog.find()
{ "_id" : "1", "blog_name" : "My First Blog", "entries" : [
{
"comments" : [
{
"user" : "bob",
"comment" : "cool"
},
{
"user" : "joe",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "this should get insetted"
}
],
"entry_id" : "123",
"entry_name" : "My first entry",
"posted" : "2010-01-01"
},
{
"entry_id" : "124",
"entry_name" : "My second entry",
"posted" : "2010-01-01",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
},
{
"entry_id" : "125",
"entry_name" : "My third entry",
"posted" : "2010-01-02",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
}
] }
> db.blog.update({ blog_name: "My First Blog", "entries.entry_id" : "123"}, { $addToSet : {'entries.$.comments' : { 'user' : 'sam', 'comment' : 'this should not get inserted, but does' } } })
> db.blog.find()
{ "_id" : "1", "blog_name" : "My First Blog", "entries" : [
{
"comments" : [
{
"user" : "bob",
"comment" : "cool"
},
{
"user" : "joe",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "this should get insetted"
},
{
"user" : "sam",
"comment" : "this should not get inserted, but does"
}
],
"entry_id" : "123",
"entry_name" : "My first entry",
"posted" : "2010-01-01"
},
{
"entry_id" : "124",
"entry_name" : "My second entry",
"posted" : "2010-01-01",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
},
{
"entry_id" : "125",
"entry_name" : "My third entry",
"posted" : "2010-01-02",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
}
] }
> db.blog.update({ blog_name: "My First Blog" }, { $addToSet : { entries : { entry_id : "123", entry_name : "My first entry", posted : "2010-01-01" } } })
> db.blog.update({ blog_name: "My First Blog" }, { $addToSet : { entries : { entry_id : "123", entry_name : "My first entry", posted : "2010-01-03" } } })
> db.blog.find()
{ "_id" : "1", "blog_name" : "My First Blog", "entries" : [
{
"comments" : [
{
"user" : "bob",
"comment" : "cool"
},
{
"user" : "joe",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "this should get insetted"
},
{
"user" : "sam",
"comment" : "this should not get inserted, but does"
}
],
"entry_id" : "123",
"entry_name" : "My first entry",
"posted" : "2010-01-01"
},
{
"entry_id" : "124",
"entry_name" : "My second entry",
"posted" : "2010-01-01",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
},
{
"entry_id" : "125",
"entry_name" : "My third entry",
"posted" : "2010-01-02",
"comments" : [
{
"user" : "jane",
"comment" : "not cool"
},
{
"user" : "sam",
"comment" : "not cool"
}
]
},
{
"entry_id" : "123",
"entry_name" : "My first entry",
"posted" : "2010-01-01"
},
{
"entry_id" : "123",
"entry_name" : "My first entry",
"posted" : "2010-01-03"
}
] }

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Wes

unread,
Apr 29, 2010, 11:51:20 PM4/29/10
to mongodb-user
I finally figured out the unique index does not prevent duplicates
within the same tree. For example, my blog #1 can have duplicate
entries.entry_id and entries.comments.user, but blog #2 can not.

Kyle Banker

unread,
Apr 30, 2010, 9:58:04 AM4/30/10
to mongod...@googlegroups.com
Created jira for this. Feel free to add comments; I think I've
described the essence of it:

http://jira.mongodb.org/browse/SERVER-1069

Reply all
Reply to author
Forward
0 new messages