简体   繁体   English

确保嵌套重复实体的索引

[英]Ensuring index for nested repeating entities

I need to enforce unique constraint on a nested document, for example: 我需要对嵌套文档实施唯一约束,例如:

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. urldisplay_urlexpaned_url必须是唯一的。 How to issue ensureIndex command for this condition in MongoDB? 如何在MongoDB中针对这种情况发出ensureIndex命令?

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? 另外,拥有这样的嵌套文档是一个好的设计,还是应该将它们移到单独的集合中并在urlEntities?从此处引用它们urlEntities? I'm new to MongoDB, any best practices suggestion would be much helpful. 我是MongoDB的新手,任何最佳实践建议都将很有帮助。

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. 当我得到另一个具有类似urlEntities对象的文档时,我只需要更新用户和计数字段。 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. 首先,我想到对urlEntities字段实施唯一约束,然后处理异常,然后进行更新,否则,如果我在插入之前检查每个条目是否存在,将对性能产生重大影响。 So, how can I enforce uniqueness in urlEntities ? 因此,如何在urlEntities实施唯一性? I tried 我试过了

{"urlEntities.display_url":1,"urlEntities.expanded_url":1},{unique:true}

But still I'm able to insert the same document twice without exceptions. 但是我仍然可以无例外地两次插入同一文档。

Uniqueness is only enforced per document. 唯一性仅针对每个文档强制执行。 You can not prevent the following (simplified from your example): 您不能阻止以下操作(从示例中简化):

db.collection.ensureIndex( { 'urlEntities.url' : 1 } );
db.col.insert( {
    _id: 42,
    urlEntities: [
        { 
            "url" : "http://t.co/ujBNNRWb0y"
        },
        { 
            "url" : "http://t.co/ujBNNRWb0y"
        } 
    ]
});

Similarily, you will have the same problem with a compound unique key for nested documents. 同样,嵌套文档的复合唯一键也会遇到相同的问题。

What you can do is the following: 可以执行以下操作:

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

Now you have the document with _id 43 with one urlEntities document. 现在,您有了带有_id 43和一个urlEntities文档的文档。 If you run the same update query again , it will not add a new array element, because the full combination of url, display_url and expanded_url already exists. 如果再次运行相同的更新查询,它将不会添加新的数组元素,因为url,display_url和expanded_url的完整组合已经存在。

Also, have a look at the $addToSet query operator's examples: http://docs.mongodb.org/manual/reference/operator/addToSet/ 另外,请查看$addToSet查询运算符的示例: http : $addToSet

for indexes on nested documents read this . 对于嵌套文档的索引,请阅读this

regarding the second part (nested documents best practices) - it really depends on your business logic and queries. 关于第二部分(嵌套文档最佳做法)-它实际上取决于您的业务逻辑和查询。 if those nested documents don't make sense as first class entities, meaning you won't be searching for them directly but only in the context of their parent document then having them nested make sense. 如果这些嵌套文档对于第一类实体没有意义,则意味着您不会直接在它们的父文档的上下文中搜索它们,而是将它们嵌套才有意义。 otherwise you should consider extracting them out. 否则,您应该考虑将它们提取出来。

i think that there isn't absolute answer to your question. 我认为您的问题没有绝对答案。 read the chapter about indexing... it helped me a lot. 阅读有关索引的章节...对我有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM