dqkxo44488 2017-03-30 08:21
浏览 60

php中的mongodb自动增量

I am using php and mongodb. I want to autoincreement the id. When i am using this code

db.online.insert({
field_id: "register_id",
nextId: 0
},
{
field_id: "user_id",
nextId: 0
});


function getNextIdOnline(field){
var ret = db.online.findAndModify({
    query: { field_id: field },
    update: { $inc: { nextId: 1 } },
    new: true
});    
return ret.nextId;
}

db.online.insert({
 _id: getNextIdOnline("register_id"),
 userid: "8223456",
 username:"Kumar"
});

It is working as fine in mongodb terminal. I want to call to function in php. when using this code

$mng1 = new MongoDB\Driver\Manager("mongodb://user:pass@localhost:27017/db");
$bulk = new MongoDB\Driver\BulkWrite;
    $proccode = 'getNextIdOnline("register_id")';
$bulk->update(['userid' => $userphone], ['_id' =>  new MongoCode($proccode),'userid' => $userphone, 'username' => $username, 'active_time' => $today], ['upsert' => 'true']);

$mng->executeBulkWrite("db.online", $bulk);

Then it shows the error is

Uncaught Error: Class 'MongoCode' not found.

I got the reference from http://pointbeing.net/weblog/2010/08/getting-started-with-stored-procedures-in-mongodb.html

How can auto increement the field using php? Please help me.

Is there any substitute for mongocode?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用三极管设计一个单管共射放大电路
    • ¥20 fluent无法启动
    • ¥15 孟德尔随机化r语言运行问题
    • ¥15 pyinstaller编译的时候出现No module named 'imp'
    • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
    • ¥15 怎么把多于硬盘空间放到根目录下
    • ¥15 Matlab问题解答有两个问题
    • ¥15 LCD12864中文显示
    • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
    • ¥15 gsoap生成onvif框架