The max value of BSONObj::getIntField()

170 views
Skip to first unread message

JohnHenry

unread,
Mar 27, 2012, 5:36:03 AM3/27/12
to mongodb-user
Hi, all
I want to know what the BSONObj function getIntField will return,
int32 or int64? If I want to store or get a number of int64 (long
long ), Is there any interface in BSONObj class? I don't want to use
double type. and there is no direct getDoubleField listed in BSONObj
members.

David Schneider

unread,
Mar 27, 2012, 10:15:11 AM3/27/12
to mongod...@googlegroups.com
It looks like your referring to the C++ mongoclient

BSONObj::getIntField returns always int32. All numbers (32-bit integer
(type 16), 64-bit integer (type 18) and floating point (type 1)) get
converted to int32. If the field is another type, the
std::numeric_limits< int >::min()=-32767 gets returned.
If the value stored in the BSON object is bigger than INT_MAX=32767 you
will get an undefined behavior. So this operation is not save as long
you know exactly how big your numbers can become.

There is no direct way to retrieve an int64 (long long) from a BSONObj.
You have to get the BSONElement (getField) first. Than you can call
BSONElement::Long(), if you know your field is always from type 64-bit
integer or call BSONElement::numberLong() which converts from any number
type or returns 0 if it's not a number type.

Be careful: getField() only returns a kind of pointer into the BSONObj.
That means you have to store the BSONObj as long as you use the BSONElement.

David

JohnHenry

unread,
Mar 27, 2012, 11:59:33 PM3/27/12
to mongodb-user
Thank you for your suggestion!
> > members.- 隐藏被引用文字 -
>
> - 显示引用的文字 -
Reply all
Reply to author
Forward
0 new messages