但是...他们实际上并没有显示如何在PHP中执行此操作.他们只是显示mongo文档中的材料. 编辑1我在想的是尝试找到一种使用db-> command()方法运行getNextSequence()的方法...然后向我的$ location数组中添加一个新项,如下所示:$location["_id"] = value_from_db_command;解决方案您需要在$ location中添加_id字段.和_id必须包含您的ID.示例:function add_playbook_history_record($location){ $m = new MongoClient("mongodb://10.1.1.111:27017"); $db = $m->testdb; $collection = $db->testcollection; $location['_id'] = getNextSequence('playhistid') $cursor = $collection->insert($location);} 我的建议:在findAndModify中添加upsert 它将为您服务: function getNextSequence($name) { $m = new MongoClient("mongodb://10.1.1.111:27017"); // In a real project, you do not need all the time to re-create the connection $db = $m->testdb; $collection = $db->counters; $result = $collection->findAndModify( ['_id' => $name], ['$inc' => ['seq' => 1]], ['seq' => true], ['new' => true, 'upsert' => true] ); if (isset($result['seq'])) { return $result['seq']; } else { return false; } } 在真实的项目中,您不需要一直在重新创建连接您可以创建MongoDatabase(此模式为singelton)class MongoDatabase{ private function __construct(){} public static function getInstance(){...} // return MongoClient}并调用需要方法MongoDatabase::getInstance()->selectCollection('counters')->findAndModify(...)I'm following the documentation found herehttps://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/I have two counters, one called locationid and the other called playhistid.This is what they look like: { "_id" : "locationid", "seq" : 0}{ "_id" : "playhistid", "seq" : 0}I've also created the following function: function getNextSequence(name) { var ret = db.counters.findAndModify( { query: { _id: name }, update: { $inc: { seq: 1 } }, new: true } );Now I need a way to call getNextSequence("locationid") from PHP.This is what my code looks like so far: function add_playbook_history_record($location){ $m = new MongoClient("mongodb://10.1.1.111:27017"); $db = $m->testdb; $collection = $db->testcollection; $cursor = $collection->insert($location);}As you can see, I'm not calling the getNextSequence() function because I don't know how to do this in. I found a similar question on this site:How to create auto increment fieldBut... they don't actually show how to do this in PHP. They just show the material from the mongo docs.EDIT 1What I'm thinking of is trying to find a way to run the getNextSequence() using the db->command() method... and then adding a new item to my $location array like this: $location["_id"] = value_from_db_command; 解决方案 You need add _id field in $location.And _id must you inc id.Example:function add_playbook_history_record($location){ $m = new MongoClient("mongodb://10.1.1.111:27017"); $db = $m->testdb; $collection = $db->testcollection; $location['_id'] = getNextSequence('playhistid') $cursor = $collection->insert($location);}My recommendation: add upsert in findAndModifyIt will work for you: function getNextSequence($name) { $m = new MongoClient("mongodb://10.1.1.111:27017"); // In a real project, you do not need all the time to re-create the connection $db = $m->testdb; $collection = $db->counters; $result = $collection->findAndModify( ['_id' => $name], ['$inc' => ['seq' => 1]], ['seq' => true], ['new' => true, 'upsert' => true] ); if (isset($result['seq'])) { return $result['seq']; } else { return false; } }In a real project, you do not need all the time to re-create the connectionYou can create the MongoDatabase (this pattern singelton)class MongoDatabase{ private function __construct(){} public static function getInstance(){...} // return MongoClient}and call need methodMongoDatabase::getInstance()->selectCollection('counters')->findAndModify(...) 这篇关于MongoDB-如何使用自动增量功能插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 13:22