mongodb保存长整型的问题

最近用php往mongodb里面写数据的时候出现了一点小问题。就是毫秒级的时间戳保存到mongodb里面后数字被截断了。
例如:1439539213873保存后在mongo里显示为:725169713,这个不是我们想要的结果。

php代码:

$mongo = new MongoClient('mongodb://127.0.0.1:27017');
$db = $mongo->selectCollection("local", "test_data");
$data['num'] = 1439539213873;
$res = $db->insert($data);

mongo里结果:

> db.test_data.find();
{ "_id" : ObjectId("56b3036931a08a07628b4567"), "num" : 725169713 }
> 

 

解决方案:

1.查看php中mongo扩展的配置mongo.native_long,如果是0那在php.ini配置里面改为1;

2.对长整型的数字进行转换

$mongo = new MongoClient('mongodb://127.0.0.1:27017');
$db = $mongo->selectCollection("local", "test_data");
//$data['num'] = 1439539213873;
$data['num'] = new MongoInt64('1439539213873');//进行转换
$res = $db->insert($data);

mongo里结果:

> db.test_data.find();
{ "_id" : ObjectId("56b306c431a08ac80f8b4567"), "num" : NumberLong("1439539213873") }
> 

这样长的整型数字就不会被截断了。

22 Responses

  1. magnificent points altogether, you just received a logo new reader.
    What would you recommend in regards to your post that you
    made a few days in the past? Any positive?

  2. Hello are using WordPress for your blog platform? I’m new to the blog world but I’m
    trying to get started and set up my own. Do you require any coding knowledge to make
    your own blog? Any help would be greatly appreciated!

  3. If some one needs expert view concerning running a
    blog after that i recommend him/her to pay a visit this blog, Keep
    up the pleasant job.

  4. Attractive section of content. I just stumbled upon your
    blog and in accession capital to assert that I acquire in fact enjoyed account your blog
    posts. Any way I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

  5. Wonderful items from you, man. I’ve remember your stuff prior to and you are simply too magnificent.
    I actually like what you have acquired here, really like what you’re saying and the way in which through
    which you are saying it. You’re making it enjoyable
    and you continue to take care of to stay it sensible.

    I can not wait to read much more from you. That is actually a terrific web site.

  6. Howdy! I could have sworn I’ve been to this site before but after
    checking through some of the post I realized it’s
    new to me. Nonetheless, I’m definitely delighted I found it and I’ll be
    book-marking and checking back often!

  7. obviously like your website however you need to test the spelling on quite a few of your posts.
    A number of them are rife with spelling problems and I to find it very bothersome
    to tell the truth nevertheless I’ll surely come back again.

  8. An outstanding share! I have just forwarded this onto
    a co-worker who had been conducting a little homework on this.

    And he in fact bought me lunch simply because I stumbled
    upon it for him… lol. So allow me to reword this….
    Thanks for the meal!! But yeah, thanks for spending the time to discuss this matter here on your web page.

  9. I know this if off topic but I’m looking into starting my own weblog and was curious what all
    is required to get setup? I’m assuming having a blog like yours would cost a pretty penny?
    I’m not very internet savvy so I’m not 100% positive. Any
    recommendations or advice would be greatly appreciated.

    Many thanks

  10. Hey there superb blog! Does running a blog such as this take a massive amount work?
    I have very little expertise in coding but I was
    hoping to start my own blog in the near future.
    Anyway, if you have any ideas or techniques for new blog owners please share.
    I know this is off topic nevertheless I just needed to ask.
    Appreciate it!

  11. What’s Happening i’m new to this, I stumbled upon this I
    have discovered It absolutely useful and it has helped me out loads.

    I am hoping to give a contribution & assist different users like its aided me.
    Great job.

  12. Hello there, just became alert to your blog through Google, and found
    that it is really informative. I’m going to watch out for brussels.
    I will appreciate if you continue this in future.
    Numerous people will be benefited from your writing.
    Cheers!

发表评论

电子邮件地址不会被公开。 必填项已用*标注