java - 确保嵌套重复实体的索引

标签 java mongodb indexing database

我需要对嵌套文档实现唯一约束,例如:

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/

相关文章:

mongodb - 你如何在 mongodb 中对数组进行 AND 查询?

ruby-on-rails - Mongoid,查找名称包含字符串的模型

xml - SQL 服务器 : Order by XML column's node

java - Android Studio 中不存在按钮类型?

MongoDB 查询组和不同的一起

javascript - 如何根据索引获取由两个新行分隔的字符串?

pandas - 访问 pandas.DataFrame 列名中包含 '.' 的内容

java - 如何判断我的应用程序是否是第一次运行?

java - Android 线程和类行为问题

java - 从命令行运行 Java 实用程序时没有足够的存储空间来处理此命令