在MongoDB中将相关键存储为OID或字符串 [英] Store related keys as OID or strings in MongoDB

查看:56
本文介绍了在MongoDB中将相关键存储为OID或字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mongodb设置存储的数据大部分都已标准化,我存储的用户数据足以显示在帖子中.但是我仍然总是存储相关用户或相关帖子的_id以便进行投票.一些ID来自ajax调用(最终存储为字符串),而其他ID直接来自mongodb服务器端(存储在它们来自OID的数据类型中),因此,当我保存这些ID时,我会混合使用相关ID作为字符串或OID.我的问题是我应该将它们全部转换为字符串还是全部转换为OID?

My mongodb setup stores my data mostly de-normalized, I store just enough user data that I would like display with a post. But I still always store the _id of the related user or related post for say a vote. Some of the ids come from ajax calls(end up stored as strings) and others come directly from mongodb server side(get stored in the data type they come from OID), thus right now when I save these I have a mix of related ids as strings or OIDs. My questions is wether I should be converting them all to strings or all to OIDs?

推荐答案

ObjectIds.它们更加节省空间,并且ObjectIds的比较比字符串的比较要快.您应该始终将输入的字符串转换回ObjectId(例如oid = new ObjectId(string)).我真的想不出想要字符串的异常.

ObjectIds. They're more space efficient and ObjectIds compares are faster than string compares. You should always convert the incoming string back to an ObjectId (e.g. oid = new ObjectId(string)). I can't really think of an exception where you'd want strings.

这篇关于在MongoDB中将相关键存储为OID或字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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