开发者

How do you query for, or insert as, specific types in MongoDB?

I'm using the Perl bindings for MongoDB, and it seems that when I insert numbers, they are sometimes counted as strings. When I add 0 to the number, it gets converted.

Is there a way, with the MongoDB Perl bindings, to specify the data type on insertion?

Is there also a way to query for specific types?

An example type query might look like this, if there was a "$type" function:

db.c.find({ key: { '$t开发者_开发技巧ype': 'NumberLong' } });


MongoDB does contain a $type operator for checking against bson datatype: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24type

Unfortunately in Perl you are subject to the languages semantics ... There is no Number datatype in Perl just Scalar.

If Perl has decided to store your value as a string that is how it will go to BSON and $type will likely not match it correctly.

However it does look like the Perl driver attempts to.test if Perl can treat a string its saving as a number in which case.it.is.stored such; you may want to test this along with the $test operator.


Use the looks_like_number parameter in your perl MongoDB driver http://search.cpan.org/~friedo/MongoDB-0.702.2/lib/MongoDB/BSON.pm#looks_like_number

Worked like a charm for me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜