在 MongoDB 中存储空值与根本不存储密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12403240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:49:22  来源:igfitidea点击:

Storing null vs not storing the key at all in MongoDB

mongodbmongodb-querydocument-databasenosql

提问by Zaid Masud

It seems to me that when you are creating a Mongo document and have a field {key: value}which is sometimes not going to have a value, you have two options:

在我看来,当您创建一个 Mongo 文档并且有一个{key: value}有时没有值的字段时,您有两个选择:

  1. Write {key: null}i.e. write null value in the field
  2. Don't store the key in that document at all
  1. 写入{key: null}即在字段中写入空值
  2. 根本不要将密钥存储在该文档中

Both options are easily queryable, in one you query for {key : null}and the other you query for {key : {$exists : false}}.

这两个选项都很容易查询,一个是您查询的{key : null},另一个是您查询的{key : {$exists : false}}

I can't really think of any differences between the two options that would have any impact in an application scenario (except that option 2 has slightly less storage).

我真的想不出这两个选项之间的任何差异会对应用程序场景产生任何影响(除了选项 2 的存储略少)。

Can anyone tell me if there are any reasons one would prefer either of the two approaches over the other, and why?

谁能告诉我是否有任何理由让人们更喜欢这两种方法中的任何一种,为什么?

EDIT

编辑

After asking the question it also occurred to me that indexes may behave differently in the two cases i.e. a sparse index can be created for option 2.

在问这个问题之后,我还发现索引在两种情况下的行为可能不同,即可以为选项 2 创建稀疏索引。

采纳答案by AlphaB

Indeed you have also a third possibility : key: ""(empty value)

事实上,你还有第三种可能性:( key: ""空值)

And you forget a specificity about null value. Query on key: nullwill retrieve you all document where key is null orwhere key doesn't exist.

你忘记了空值的特殊性。查询 key: null将检索您所有键为空键不存在的文档。

When a query on $exists:falsewill retrieve only doc where field key doesn't exist.

当查询$exists:false将仅检索字段键不存在的文档时。

To go back to your exact question it depends of you queries and what data represent. If you need to keep that, by example, a user set a value then unset it, you should keep the field as null or empty. If you dont need, you may remove this field.

回到你的确切问题,这取决于你的查询和数据代表什么。如果您需要保留它,例如,用户设置一个值然后取消设置它,您应该将该字段保留为空或空。如果您不需要,您可以删除此字段。

回答by Samuel García

Note that, since MongoDB doesnt use field name dictionary compression, field:nullconsumes disk space and RAM, while storing no key at all doesnt consume resources.

需要注意的是,由于 MongoDB 没有使用字段名称字典压缩,因此field:null会消耗磁盘空间和 RAM,而根本不存储任何键也不消耗资源。

回答by Sammaye

It really comes down to:

这真的归结为:

  • Your scenario
  • Your querying manner
  • Your index needs
  • Your language
  • 你的场景
  • 您的查询方式
  • 您的索引需求
  • 你的语言

I personally have chosen to store null keys. It makes it much easier to integrate into my app. I use PHP with Active Record and uisng null values makes my life a lot easier since I am not having to put the stress of field depedancy upon the app. Also I do not need to make any complex code to deal with magics to set non-existant variables.

我个人选择存储空键。它可以更轻松地集成到我的应用程序中。我将 PHP 与 Active Record 和 uisng null 值一起使用使我的生活更轻松,因为我不必将字段依赖的压力放在应用程序上。此外,我不需要编写任何复杂的代码来处理设置不存在的变量的魔法。

I personally would not store an empty value like ""since if your not careful you could have two empty values nulland ""and then you'll have a hap-hazard time of querying specifically. So I personally prefer nullfor empty values.

我个人不会像存储空值"",因为如果你不小心,你可以有两个空值null"",然后你就会有专门查询的HAP-危险时间。所以我个人更喜欢null空值。

As for space and index: it depends on how many rows might not have this colum but I doubt you will really notice the index size increase due to a few extra docs with null in. I mean the difference in storage is mineute especially if the corresponding key name is small as well. That goes for large setups too.

至于空间和索引:这取决于有多少行可能没有这个列,但我怀疑你真的会注意到索引大小会因为一些额外的 null 文档而增加。我的意思是存储的差异是微小的,特别是如果相应的键名也小。这也适用于大型设置。

I am quite frankly unsure of the index usage between $existsand nullhowever nullcould be a more standardised method by which to query the existance since remember that MongoDB is schemaless which means you have no requirement to have that field in the doc which again produces two empty values: non-existant and null. So better to choose one or the other.

坦率地说,我不确定两者之间的索引使用情况$existsnull但是null可能是一种更标准化的方法来查询存在性,因为记住 MongoDB 是无模式的,这意味着您不需要在文档中拥有该字段,该字段再次产生两个空值:不存在和null。所以最好选择其中之一。

I choose null.

我选择null

回答by Vinod

Another point you might want to consider is when you use OGM tools like Hibernate OGM.

您可能需要考虑的另一点是何时使用 Hibernate OGM 等 OGM 工具。

If you are using Java, Hibernate OGM supports the JPA standard. So if you can write a JPQL query, you would be theoretically easy if you want to switch to an alternate NoSQL datastore which is supported by the OGM tool.

如果您使用的是 Java,Hibernate OGM 支持 JPA 标准。因此,如果您可以编写 JPQL 查询,那么如果您想切换到 OGM 工具支持的备用 NoSQL 数据存储,理论上您会很容易。

JPA does not define a equivalent for $exists in Mongo. So if you have optional attributes in your collection then you cannot write a proper JPQL for the same. In such a case, if the attribute's value is stored as NULL, then it is still possible to write a valid JPQL query like below.

JPA 没有为 Mongo 中的 $exists 定义等效项。因此,如果您的集合中有可选属性,那么您将无法为其编写正确的 JPQL。在这种情况下,如果属性的值存储为 NULL,那么仍​​然可以编写一个有效的 JPQL 查询,如下所示。

SELECT p FROM pppoe p where p.logout IS null;