MongoDB的preferred架构为嵌入式集合。文档与阵列 [英] MongoDB preferred schema for embedded collections. documents vs. arrays

查看:85
本文介绍了MongoDB的preferred架构为嵌入式集合。文档与阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信至少有两种方法已嵌入的mongodb文档中的数据。在简化的情况下,我们可能有这样的事情:

I believe there at least two ways to have embedded data in a mongodb document. In a simplified case we could have something like this:

{
    'name' : 'bill',
    'lines': {
       'idk73716': {'name': 'Line A'},
       'idk51232': {'name': 'Line B'},
       'idk23321': {'name': 'Line C'}
    }
}

和一个数组:

{
    'name' : 'bill',
    'lines': [
       {'id': 'idk73716', 'name': 'Line A'},
       {'id': 'idk51232', 'name': 'Line B'},
       {'id': 'idk23321', 'name': 'Line C'}
    ]
}

正如你在这个用例看到它让每一行的id是非常重要的。

As you can see in this use case it's important to keep the id of each line.

如果有这两种模式之间的利弊,我不知道。特别是当它涉及到使用索引我有一种感觉,第二个可能是更容易为人们可以创建lines.id甚至lines.name'的索引来搜索一个id或名称翻过所有文件的工作。我没有发现在第一个例子中的任何工作溶液到索引的id('idk73716'等等)。

I'm wondering if there are pros and cons between these two schemas. Especially when it comes to using indexes I have the feeling that the second may be easier to work with as one could create an index on 'lines.id' or even 'lines.name' to search for an id or name accross all documents. I didn't find any working solution to index the ids ('idk73716' and so on) in the first example.

时一般$ pferred如果你有一个用例这样使用第二种方法p $?

Is it generally preferred to use the second approach if you have a use case like this?

推荐答案

在你的第一个方法,你不能索引ID字段,因为使用id作为重点。它有点像键值字典行为。如果你有组已知的IDS(当然少一些).Assume在你的第一个例子中,ID是在前面众所周知此方法非常有用,

In your first approach you can't index the id fields, since id used as key. Its kind of act like key value dictionary. This approach is useful if you have the known set of ids (of course less number).Assume In your first example the id is well known at front ,

>>db.your_colleection.find()
 { "_id" : ObjectId("4ebbb6f974235464de49c3a5"), "name" : "bill", 
  "lines" : { 
             "idk73716" : { "name" : "Line A" },
             "idk51232" : { "name" : "Line B" } ,
             "idk23321":  { "name" : "Line C" }
            } 
  }

所以要找到id字段idk73716的值,可以通过

so to find the values for id field idk73716, you can do this by

 db.your_colleection.find({},{'lines.idk73716':1})
 { "_id" : ObjectId("4ebbb6f974235464de49c3a5"), "lines" : { "idk73716" : { "name" : "Line A" } } }

空{}表示将查询,而第二部分{'lines.idk73716':1}是一个查询选择器。

the empty {} denotes the query, and the second part {'lines.idk73716':1} is a query selector.

具有单独挑选特定领域的优势,有钥匙的ID 虽然{'lines.idk73716':1}是一个字段选择器,在这里它作为一个查询和选择。但这不能在你的第二个方法来完成。假设第二个集合是一种像这样

having ids as keys having an advantage of picking the particular field alone. Even though {'lines.idk73716':1} is a field selector, here it serves as a query and selector. but this cannot be done in your second approach. Assume the second collection is kind of like this

> db.second_collection.find()
{ "_id" : ObjectId("4ebbb9c174235464de49c3a6"), "name" : "bill", "lines" : [
    {
        "id" : "idk73716",
        "name" : "Line A"
    },
    {
        "id" : "idk51232",
        "name" : "Line B"
    },
    {
        "id" : "idk23321",
        "name" : "Line C"
    }
] }
> 

和你索引的字段id,所以如果你想通过编号来查询

And you indexed the field id, so if you want to query by id

> db.second_collection.find({'lines.id' : 'idk73716' })

{ "_id" : ObjectId("4ebbb9c174235464de49c3a6"), "name" : "bill", "lines" : [
    {
        "id" : "idk73716",
        "name" : "Line A"
    },
    {
        "id" : "idk51232",
        "name" : "Line B"
    },
    {
        "id" : "idk23321",
        "name" : "Line C"
    }
] }
> 

通过观察上述输出,其可见,没有办法单独挑匹配子(嵌入)的文件,但也可以在第一种方法。这是MongoDB的默认行为。

by seeing the above output, its visible that there is no way to pick the matching sub(embedded) documents alone, but it is possible in the the first approach. This is the default behavior of mongodb.

db.second_collection.find({'lines.id' : 'idk73716' },{'lines':1})

将获取所有行,而不仅仅是idk73716

will fetch all lines, not just idk73716

{ "_id" : ObjectId("4ebbb9c174235464de49c3a6"), "lines" : [
    {
        "id" : "idk73716",
        "name" : "Line A"
    },
    {
        "id" : "idk51232",
        "name" : "Line B"
    },
    {
        "id" : "idk23321",
        "name" : "Line C"
    }
] }

希望这有助于

修改

感谢 @Gates副总裁获得指出

db.your_collection.find({'lines.idk73716:{$存在:真}})。如果你
  要使用ID为键版本中,存在的查询将工作,但
  它不会是可转位的

db.your_collection.find({'lines.idk73716':{$exists:true}}). If you want to use the "ids as keys" version, the exists query will work, but it will not be indexable

我们仍然可以使用$存在查询ID,但它不会是可转位的

We still can use $exists to query the id, but it will not be indexable

这篇关于MongoDB的preferred架构为嵌入式集合。文档与阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆