如何使用Node.js从Mongodb中提取NumberLong数据?

编程入门 行业动态 更新时间:2024-10-22 09:43:48
本文介绍了如何使用Node.js从Mongodb中提取NumberLong数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Node.js,并尝试使用以下代码将时间戳数据存储在Mongodb数据库中:

I am using Node.js and tried to store the timestamp data in the Mongodb Database with the folowing code:

`articleProvider.saveUser({ userID : user, email : email, address : "", time : new Date().getTime(), },function(error,userData){ if(!userData){ callback(error);} else{ callback(null,userData);} }); } });`

存储在数据库中的数据是(来自mongo shell):

And the data stored in the Database is (from mongo shell):

{ "userID" : "hem", "email" : "hereshem@gmail", "address" : "Kathmandu", "time" : NumberLong("1320911838254"), "_id" : ObjectId("4ebb83dea7dd40990e000002") }

但是,如果我在node.js控制台中检索数据,输出将变为:

BUT if I retrieve the data in node.js console the output becomes :

{ userID: 'hem', email: 'hereshem@gmail', address: 'Kathmandu', time: { low_: -1938088914, high_: 307 }, _id: 4ebb83dea7dd40990e000002 }

现在我的问题是如何使用NumberLong数据检索字段"time"的值?

Now My Question is How to Retrieve this value of field "time" with NumberLong data?

它为输出提供了两个不同的字段"low_"和"high_".但是,我只希望输出"1320911838254".

It gives the output with two different fields of "low_" and "high_". But, I want the output only "1320911838254" .

有人可以帮助我吗?

感谢您的回复.但是我尝试使用

Thanks for the response. But I tried to get the number using

var value = new Long(low_bits, high_bits).toNumber();

Lycha先生发布的内容,但在Node.js平台中显示为未定义Long"错误:

as Mr. Lycha had posted, but in Node.js Platform it shows an error "Long is not defined" as :

var value = new Long(user.time.low_, user.time.high_).toNumber();

ReferenceError: Long is not defined at /home/developer/Desktop/Express/app.js:65:1 at /home/developer/Desktop/Express/public/js/processData.js:76:10 at /home/developer/Desktop/Express/public/js/dbQuery.js:196:16 at [object Object].<anonymous> (/home/developer/node_modules/mongodb/lib/mongodb/collection.js:743:5) at [object Object].emit (events.js:67:17) at [object Object].<anonymous> (/home/developer/node_modules/mongodb/lib/mongodb/connections/server.js:97:12) at [object Object].emit (events.js:64:17) at Socket.<anonymous> (/home/developer/node_modules/mongodb/lib/mongodb/connection.js:161:16) at Socket.emit (events.js:64:17) at Socket._onReadable (net.js:678:14)

如何在node.js中获取长解析器?有想法吗?

How to get the long parser in node.js? Any Idea?

并感谢strada的回应.但是如何在Mongodb中存储为dateObject? DataType本身是定义的.有什么方法可以更改Mongodb中的数据类型吗? 以及如何解析呢?

And thanks strada for response. But how to store as dateObject in Mongodb? The DataType is itself defined. Is there any method to change the Datatype in Mongodb? And How to Parse it?

推荐答案

经过长时间的练习,我找到了最佳解决方案:

I have found the best solution after the long practice :

articleProvider.saveUser({ userID : user, email : email, address : "", time : new Date().getTime().toString(), },function(error,userData){ if(!userData){ callback(error);} else{ callback(null,userData);} }); } });

使用"toString()"将日期对象存储为字符串对象,并在查询后使用"parseInt()"将字符串对象更改为整数值.

Store the date object as the string object using "toString()" and after the query change the string object into the integer value using "parseInt()".

我认为这确实是简单且很好的解决方案.

I think this is really easy and good solution.

更多推荐

如何使用Node.js从Mongodb中提取NumberLong数据?

本文发布于:2023-11-22 19:21:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   数据   Node   js   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!