我需要对嵌套文档实现唯一约束,例如:
urlEntities: [ { "url" : "http://t.co/ujBNNRWb0y" , "display_url" : "bit.ly/11JyiVp" , "expanded_url" : "http://bit.ly/11JyiVp"} , { "url" : "http://t.co/DeL6RiP8KR" , "display_url" : "ow.ly/i/2HC9x" , "expanded_url" : "http://ow.ly/i/2HC9x"} ]
url
, display_url
, and expaned_url
need to be unique. How to issue ensureIndex
command for this condition in MongoDB?
Also, is it a good design to have nested documents like this or should I move them to a separate collection and refer them from here inside urlEntities?
I'm new to MongoDB, any best practices suggestion would be much helpful.
Full Scenario:
Say if I have a document as below in the db which has millions of data:
{ "_id" : { "$oid" : "51f72afa3893686e0c406e19"} , "user" : "test" , "urlEntities" : [ { "url" : "http://t.co/64HBcYmn9g" , "display_url" : "ow.ly/nqlkP" , "expanded_url" : "http://ow.ly/nqlkP"}] , "count" : 0}
When I get another document with similar urlEntities
object, I need to update user and count fields only. First I thought of enforcing unique constraint on urlEntities
fields and then handle exception and then go for an update, else if I check for each entry whether it exists before inserting, it will have significant impact on the performance. So, how can I enforce uniqueness in urlEntities
? I tried
{"urlEntities.display_url":1,"urlEntities.expanded_url":1},{unique:true}
但我仍然可以毫无异常(exception)地插入同一个文档两次。
最佳答案
仅对每个文档强制执行唯一性。您无法阻止以下情况(从您的示例中简化):
db.collection.ensureIndex( { 'urlEntities.url' : 1 } );
db.col.insert( {
_id: 42,
urlEntities: [
{
"url" : "http://t.co/ujBNNRWb0y"
},
{
"url" : "http://t.co/ujBNNRWb0y"
}
]
});
同样,嵌套文档的复合唯一键也会遇到同样的问题。
您可以执行以下操作:
db.collection.insert( {
_id: 43,
title: "This is an example",
} );
db.collection.update(
{ _id: 43 },
{
'$addToSet': {
urlEntities: {
"url" : "http://t.co/ujBNNRWb0y" ,
"display_url" : "bit.ly/11JyiVp" ,
"expanded_url" : "http://bit.ly/11JyiVp"
}
}
}
);
现在您已拥有 _id 43
的文档以及一个 urlEntities 文档。如果您再次运行相同的更新查询,它将不会添加新的数组元素,因为 url、display_url 和 Expanded_url 的完整组合已经存在存在。
此外,请查看 $addToSet
查询运算符的示例:http://docs.mongodb.org/manual/reference/operator/addToSet/
关于java - 确保嵌套重复实体的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17931586/