在 mongo 中保存时如何确保特定的数据类型?

发布于 2024-10-08 08:29:35 字数 209 浏览 0 评论 0原文

我正在使用 mongodb。

我正在保存一个包含很长数字的记录,但我不想将其保存为数字(浮点数),我希望将其保存为字符串。

例如,我保存了“171829572137423434”,得到了“1.718295E+16”(只是一个例子)但是我需要完整的数字,因为它是一个ID,如何强制将其保存为mongodb中的字符串?

顺便说一句,我正在使用 PHP api。

I'm using mongodb.

I'm saving a record with a very long number, but I don't want to save it as a number (a float number) I want it as a string.

e.g. I saved "171829572137423434" and I got "1.718295E+16" (just an example) But I need the complete number since is an ID, how can I force to save it as a string in mongodb?

By the way I am using PHP api.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2024-10-15 08:29:35
$number = 42;

// this stores as a number
$collection->insert(array('number'=>$number));

// this stores as a string
$collection->insert(array('number'=>strval($number)));
$number = 42;

// this stores as a number
$collection->insert(array('number'=>$number));

// this stores as a string
$collection->insert(array('number'=>strval($number)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文