MongoDB存储ObjectId的数组 [英] MongoDB storing arrays of ObjectId's

查看:212
本文介绍了MongoDB存储ObjectId的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我必须存储一个对象ID数组。我应该使用什么?例如:
$ b $ pre $ [ObjectId(50350e12a36feb1be6000364),ObjectId(57350e12a37fef1be6000922),ObjectId(10350e17d34ffb1be6200925)]

或类似的东西:



<$ p $
























$ b $我可以节省第二个空间,然后将其转换为 ObjectId ,但是使用这种方法是否会丢失任何东西?

解决方案

肯定会用第一种方法,直接存储 ObjectId s。这节省了空间,如 ObjectId 是12个字节,而第二个方法字符串是24个字节。另外,如果稍后使用 ObjectId s来获取对象,则存储为 ObjectId 节省了一些麻烦。


In my database I have to store an array of object ids. What should I use? Something like this:

[ObjectId("50350e12a36feb1be6000364"), ObjectId("57350e12a37fef1be6000922"), ObjectId("10350e17d34ffb1be6200925")]

or something like this:

["50350e12a36feb1be6000364", "57350e12a37fef1be6000922", "10350e17d34ffb1be6200925"]

I could save space with the second, and then cast to ObjectId, but am I loosing anything by using this approach? Do ObjectIds behave like foreign keys in relational databases?

解决方案

I would definitely go with the first approach, storing the ObjectIds directly. This saves space, as ObjectId is 12 bytes whereas the second approach string is 24 bytes.

Also, if the ObjectIds are used to fetch the objects later, storing as ObjectId saves some hassle.

这篇关于MongoDB存储ObjectId的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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