php实现Mongodb自定义方式生成自增ID的方法_php技巧
本文实例讲述了php实现Mongodb自定义方式生成自增ID的方法。分享给大家供大家参考。具体分析如下:
>db.ids.save({name:"user", id:0});
//可以查看一下是否成功
> db.ids.find();
{ "_id" : ObjectId("4c637dbd900f00000000686c"), "name" : "user", "id" : 0 }
//然后每次添加新用户之前自增一下 ids集合 获得id
>userid = db.ids.findAndModify({update:{$inc:{'id':1}}, query:{"name":"user"}, new:true});
{ "_id" : ObjectId("4c637dbd900f00000000686c"), "name" : "user", "id" : 1 }
//注:因为findAndModify是一个方法完成更新查找两个操作,所以具有原子性,多线程不会冲突。
//然后保存相应的数据
>db.user.save({uid:userid.id, username:"kekeles", password:"kekeles", info:"http://www.jb51.net/ "});
//查看结果
> db.user.find();
{ "_id" : ObjectId("4c637f79900f00000000686d"), "uid" : 1, "username" : "admin", "password" : "admin" }
//这是mongo的shell,如果用的是服务器端程序java php python,可以自己对这些操作封装一下,只用传几个参数就可以返回自增的id,还可以实现像Oracle的跨表的自增id。
自己写了一段php的,拿出来给大家分享。
<?php function 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.jb51.net/ ' )); $conn->close(); ?>
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
