为什么php往mongo int里插入整型数字8,变成了numberint

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&Happiness is Grasp now!
困扰了我一天的问题。首先是Mongodb副本集。查php的Mongo扩展手册把副本集搞定。然后又是长整型时间的问题。
存进mongo的是java的长整型时间戳。而php是弱类型语言,且php的时间戳是11位。目前还是10位的阶段。转换成java的时间戳需要在后面补零。
// 生成java时间戳
$time = (string)$time.'000';
$time = (int)$
但是查询结果根本没有数据。把框架的Mongo用Debug打印出查询语句,没有问题。把查询语句直接在shell里查也没问题。但就是没数据。
后来打开Mongo的日志,调整级别为5,这样会记录查询语句。参照。
发现我所查询的时间都变成了负数:“{ $gt: - }”
然后万能的google找到了答案。php的长整型需要使用MongoInt64这个类来转换一下。虽然你在php中输出结果是一样的,但查询起来就不一样了。那么在上面的语句后面再加上一句就对了。
// 必须的一步,将长整形使用MongoInt64转换成64位整型
$time = new MongoInt64($time);
Mongo扩展的类参考在这里:
This entry was posted in
and tagged . Bookmark the .
Categories<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&Keyboard Shortcuts?
Next menu item
Previous menu item
Previous man page
Next man page
Scroll to bottom
Scroll to top
Goto homepage
Goto search(current page)
Focus search box
Change language:
Brazilian Portuguese
Chinese (Simplified)
MongoCursor::limit
MongoCursor::limit & Limits the number of results returned
Description
MongoCursor::limit
( int $num
Parameters
The number of results to return.
Return Values
Returns this cursor.
Errors/Exceptions
if this cursor has started iterating.
MongoDB core docs on .
- Limits the number of elements returned in one batch.
- Skips a number of results
//Show only unique result$cursor = $collection-&find();$cursor-&limit(1);type conversion - How do I convert a string to a number in PHP? - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP?
'0.3454545'
21.2k11109141
2,963102040
You don't typically need to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type,
$num = "3.14";
$int = (int)$
$float = (float)$
293k42369511
There are a few ways to do so:
Cast the strings to numeric primitive data types:
$num = (int) "10";
$num = (double) "10.12"; // same as (float) "10.12";
Perform math operations on the strings:
$num = "10" + 1;
$num = floor("10.1");
$num = intval("10");
$num = floatval("10.1");
10.9k22853
In whatever (loosely-typed) language you can always cast a string to a number by adding a zero to it.
However, there is very little sense in this as PHP will do it automatically at the time of using this variable, and it will be cast to a string anyway at the time of output.
Note that you may wish to keep dotted numbers as strings, because after casting to float it may be changed unpredictably, due to float numbers' nature.
10.1k1269106
You can use:
(int)(your value);
Or you can use:
intval(string)
10.1k1269106
2,07542859
If you want get float for $value = '0.4', but int for $value = '4', you can write:
$number = ($value == (int) $value) ? (int) $value : (float) $
Little bit dirty, but it works.
To avoid problems try intval($var). Some examples:
echo intval(42);
echo intval(4.2);
echo intval('42');
echo intval('+42');
echo intval('-42');
echo intval(042);
echo intval('042');
echo intval(1e10);
echo intval('1e10');
echo intval(0x1A);
echo intval();
echo intval();
echo intval(''); //
echo intval(42, 8);
echo intval('42', 8);
echo intval(array());
echo intval(array('foo', 'bar'));
You can use (int)_value_ for example.
$string = '100';
$int = (int)$
4,30112543
Just a little note to the answers that can be useful and safer in some cases.
You may want to check if the string actually contains a valid numeric value first and only then convert it to a numeric type (for example if you have to manipulate data coming from a db that converts ints to strings). You can use is_numeric() and then floatval():
$a = "whatever"; // any variable
if (is_numeric($a))
var_dump(floatval($a)); // type is float
var_dump($a); // any type
you can use intval(string) or floatval(string) functions to convert strings to numbers.
10.1k1269106
I've found that in JavaScript a simple way to convert a string to a number is to multiply it by 1. It resolves the concatenation problem, because the "+" symbol has multiple uses in JavaScript, while the "*" symbol is purely for mathematical multiplication.
Based on what I've seen here regarding PHP automatically being willing to interpret a digit-containing string as a number (and the comments about adding, since in PHP the "+" is purely for mathematical addition), this multiply trick works just fine for PHP, also.
I have tested it, and it does work... Although depending on how you acquired the string, you might want to apply the trim() function to it, before multiplying by 1.
10.1k1269106
$num = "3.14";
$int = (int)$ // output = 3
$float = (float)$ // output = 3.14
$double = (double)$ // output = 3.14
you can convert a string to a number in PHP using cast .
Here is a function I wrote to simplify things for myself:
It also returns shorthand versions of boolean, integer, double and real.
function type($mixed, $parseNumeric = false)
if ($parseNumeric && is_numeric($mixed)) {
//Set type to relevant numeric format
$mixed += 0;
$t = gettype($mixed);
switch($t) {
case 'boolean': return 'bool'; //shorthand
case 'integer': return 'int';
//shorthand
case 'double': case 'real': return 'float'; //equivalent for all intents and purposes
default: return $t;
Calling type with parseNumeric set to true will convert numeric strings before checking type.
type("5", true) will return int
type("3.7", true) will return float
type("500") will return string
Just be careful since this is a kind of false checking method and your actual variable will still be a string. You will need to convert the actual variable to the correct type if needed. I just needed it to check if the database should load an item id or alias, thus not having any unexpected effects since it will be parsed as string at run time anyway.
If you would like to detect if objects are functions add this case to the switch:
case 'object': return is_callable($mixed)?'function':'object';
Instead of having to choose weather to convert the string to int or float. You can simply add a 0 to it and PHP will automatically convert the result to a numeric type.
// being sure the string is actually a number
if( is_numeric($string) )
$number = $string + 0;
else // let's the number be 0 if the string is not a number
$number = 0;
$a = "10";
$b = (int)$a;
You can use this to convert a string to an int in .
10.1k1269106
Try using something like this:
$string='5';// $ string is a variable to hold string '5'
$int=intval($string);// $int is the converted string to integer
$int=intval('5');
2,09752657
You can change the data type as follows
$number = "1.234";
echo gettype ($number) . "\n"; //Returns string
settype($number , "float");
echo gettype ($number) . "\n"; //Returns float
For historical reasons "double" is returned in case of a float.
3,92451334
You can use:
((int) $var)
( but in big number it return
But the best solution is to use:
if (is_numeric($var))
$var = (isset($var)) ? $var : 0;
if (is_numeric($var))
$var = (trim($var) == '') ? 0 : $
10.1k1269106
For your specific case, you could just multiply by 1 to convert string to number in php. PHP takes care of the floating and integer automatically.
$num = "2.12";
var_dump(is_float($num)); //bool(false)
var_dump(is_int($num)); //bool(false)
var_dump(is_string($num)); //bool(true)
$num = $num*1;
var_dump(is_float($num)); //bool(true)
var_dump(is_int($num)); //bool(false)
var_dump(is_string($num)); //bool(false)
PHP will do it for you within limits
$str="3.148";
printf("%f\n", $num);
11.2k22444
all suggestions loose the numeric type
this seems to me a best practice
function str2num($s){
//returns a num or FALSE
$return_value =
!is_numeric($s) ? false :
(intval($s)==floatval($s)) ? intval($s) :floatval($s);
print "\nret=$return_value type=".gettype($return_value)."\n";
Here is the function that achieves what you are looking for. First we check if the value can be understood as a number, if so we turn it into an int and a float.
If the int and float are the same (e.g., 5 == 5.0) then we return the int value.
If the int and float are not the same (e.g., 5 != 5.3) then we assume you need the precision of the float and return that value.
If the value isn't numeric we throw a warning and return null.
function toNumber($val) {
if (is_numeric($val)) {
$int = (int)$
$float = (float)$
$val = ($int == $float) ? $int : $
trigger_error("Cannot cast $val to a number", E_USER_WARNING);
Different Approach:
push it all into an array / associative array.
json_encode($your_array, JSON_NUMERIC_CHECK ); optionally decode it back
10.3k62045
user257319
You can always add zero to it!
'2.34' + 0
2.34 (float)
'0.3454545' + 0
0.3454545 (float)
$a = "7.2345";
$b = round(((float)$a),0);
32.2k114272
protected by &#9830;
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10
on this site (the ).
Would you like to answer one of these
Not the answer you&#39;re looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 mongo update int 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信