php中的mongodb自动递增
我正在使用php和mongodb。我想在使用此代码时自动递增idphp中的mongodb自动递增,php,mongodb,Php,Mongodb,我正在使用php和mongodb。我想在使用此代码时自动递增id 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:
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"
});
它在mongodb终端工作正常。我想在php中调用函数。使用此代码时
$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);
然后显示错误为
未捕获错误:未找到类“MongoCode”
我从他那里得到了推荐信
如何使用php自动递增字段?请帮帮我
mongocode有什么替代品吗 MongoCode来自legacy driver,use instead如何在phpSorry Fazil中使用,我的注释是注释而不是答案。我只是指出,您所指的文章已经有7年历史了,
MongoCode
早就被弃用了。在代码段Mongodb\driver\Manager
的第一行中使用的新的php Mongodb驱动程序没有MongoCode
类,因此出现了错误。我已经提供了一个参考的替代类在新的驱动程序与真诚的希望,与小的努力,你可以回答自己的问题。但我没有得到任何解决办法。有没有mongocode的替代品?嗯,好的,我可以重复第三遍,MongoDB\BSON\Javascript是mongocode的替代品。希望能有帮助。