Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store unsigned long long (uint64_t) values in a MongoDB document?

I want to store numbers of type unsigned long long (uint64_t) in a MongoDB document, how do I do it?

I need to use unsigned long long because I'm using Twitter API which uses unsigned 64 bit integers https://dev.twitter.com/docs/twitter-ids-json-and-snowflake

The range of the the unsigned 64 bit integral type needs to be represended by 8 bytes and with a data range of 0 to 18,446,744,073,709,551,615.

I'm using the C++ MongoDB driver and the append member function of the BSONArrayBuilder class doesn't have an overload for unsigned long long, only for long long.

Here's the error G++ 4.7.2 spits out when I try to call arrayBuilder.append(id), with an id of type uint64_t:

MongoDB/mongo/db/../bson/bson-inl.h:342:9: error: call of overloaded ‘append(const char*&, long long unsigned int&)’ is ambiguous
MongoDB/mongo/db/../bson/bsonobjbuilder.h:167:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, bool)
MongoDB/mongo/db/../bson/bsonobjbuilder.h:175:25: note: virtual mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, int)
MongoDB/mongo/db/../bson/bsonobjbuilder.h:183:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, unsigned int)
MongoDB/mongo/db/../bson/bsonobjbuilder.h:188:25: note: virtual mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, long long int)
MongoDB/mongo/db/../bson/bsonobjbuilder.h:244:25: note: virtual mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, double)
MongoDB/mongo/db/../bson/bsonobjbuilder.h:324:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, mongo::Date_t)
  • I know that the BSON specification has int64 defined as 8 bytes (64-bit signed integer).
  • I do NOT want to use a string for the ID.
like image 594
Alex Bitek Avatar asked Dec 21 '12 17:12

Alex Bitek


People also ask

How do you store data in MongoDB?

Documents are used to store data in MongoDB. These documents are saved in JSON (JavaScript Object Notation) format in MongoDB. JSON documents support embedded fields, allowing related data and data lists to be stored within the document rather than in an external table. JSON is written in the form of name/value pairs.

What is the limitation of a document in MongoDB?

The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

Can you store objects in MongoDB?

Large objects, or "files", are easily stored in MongoDB. It is no problem to store 100MB videos in the database.

Is Ulong a UInt64?

In C#, UInt64 struct is used to represent 64-bit unsigned integers(also termed as the ulong data type) starting from range 0 to 18,446,744,073,709,551,615.


2 Answers

Because MongoDB/BSON only supports signed 64-bit integers, you'll need to cast your 64-bit unsigned values to/from long long when interacting with the database.

All 64-bits are still used so you won't lose any of the available unsigned range, it will just show as a negative value in the database for those values that use the most significant bit.

like image 61
JohnnyHK Avatar answered Nov 15 '22 05:11

JohnnyHK


With all due respect, IMHO the best thing you can do is to reconsider your decision not to use string for the ID since:

  1. probably it is much more efficient than you ever though due to the amount of overhead of bson internals when storing 64B double which usually is 3 additional fields and field names (see 'floatApprox','top', bottom' here , here and here)

  2. it is already provided as id_str by twitter API you are using for good reason

  3. you are going to have real trouble casting those numbers in JS if you ever happen to use mongodb map reduce

like image 39
nickmilon Avatar answered Nov 15 '22 06:11

nickmilon