Php implements the Mongodb custom method to generate the auto-increment ID, mongodbid. Php implements the Mongodb custom method to generate the auto-increment ID. mongodbid this article describes how php implements the Mongodb custom method to generate the auto-increment ID. Share it with you for your reference. Specifically, php implements the Mongodb custom method to generate the auto-increment ID, mongodbid
This example describes how to generate a user-defined ID for Mongodb in php. Share it with you for your reference. The specific analysis is as follows:
The code is as follows: // first create an auto-increment id set ids
> Db. ids. save ({name: "user", id: 0 });
// Check whether the operation is successful.
> Db. ids. find ();
{"_ Id": ObjectId ("4c637dbd900f00000000686c"), "name": "user", "id": 0}
// Then, the ids are automatically added to the ids set each time a new user is added.
> Userid = db. ids. findAndModify ({update: {$ inc: {'id': 1 }}, query: {"name": "user"}, new: true });
{"_ Id": ObjectId ("4c637dbd900f00000000686c"), "name": "user", "id": 1}
// Note: Because findAndModify is a method that completes the update and search operations, it is atomic and does not conflict with multithreading.
// Save the corresponding data
> Db. user. save ({uid: userid. id, username: "kekeles", password: "kekeles", info: "http://www.bkjia.com /"});
// View the result
> Db. user. find ();
{"_ Id": ObjectId ("4c637f79900f00000000686d"), "uid": 1, "username": "admin", "password": "admin "}
// This is the mongo shell. if you use the server-side java php python program, you can encapsulate these operations by yourself. only a few parameters are required to return the auto-increment id, you can also implement auto-increment IDs across tables like Oracle.
I wrote a php article and shared it with you.
<?phpfunction mid($name, $db){$update = array('$inc'=>array("id"=>1));$query = array('name'=>$name);$command = array('findandmodify'=>'ids', 'update'=>$update,'query'=>$query, 'new'=>true, 'upsert'=>true);$id = $db->command($command);return $id['value']['id'];}$conn = new Mongo();$db = $conn->idtest;$id = mid('user', $db);$db->user->save(array('uid'=>$id, 'username'=>'kekeles', 'password'=>'kekeles', 'info'=>'http://www.bkjia.com/ '));$conn->close();?>
I hope this article will help you with php programming.
Examples in this article describes how to generate a user-defined ID for Mongodb in php. Share it with you for your reference. Details...