5

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);}
            });
        }
    });`

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

{ "userID" : "hem", "email" : "[email protected]", "address" : "Kathmandu", "time" : NumberLong("1320911838254"), "_id" : ObjectId("4ebb83dea7dd40990e000002") }

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

{ userID: 'hem',
  email: '[email protected]',
  address: 'Kathmandu',
  time: { low_: -1938088914, high_: 307 },
  _id: 4ebb83dea7dd40990e000002 }

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

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

Can Anyone Help me?


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

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

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)

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

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?

2 Answers 2

3

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);}
        });
    }
});

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.

Sign up to request clarification or add additional context in comments.

Comments

1

MongoDB stores the number as 64bit-object but javascript doesn't support that, that's why they have to have the _low and _high. You can get the number like this:

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

Read here for more. You should consider using the date object instead.

1 Comment

I'm trying to do that in the Mongo Shell in Compass, and I get: Long is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.