mongodb php auto increment 自增
mongodb的自增实现根oracle,postgresql是差不多,都是通过计数器来实现的. oracle自增实现: 实例说明oracle序列用法 postgresql自增实现:? postgresql auto_increment 实现 通用方法 1,mongodb命令行下实现auto_increment db.counters.insert( //计数器表 { _
mongodb的自增实现根oracle,postgresql是差不多,都是通过计数器来实现的.
oracle自增实现: 实例说明oracle序列用法
postgresql自增实现:?postgresql auto_increment 实现 通用方法
1,mongodb命令行下实现auto_increment
> db.counters.insert( //计数器表 { _id: "userid", seq: 0 } ); WriteResult({ "nInserted" : 1 }) > db.counters.find(); { "_id" : "userid", "seq" : 0 } > function getNextSequence(name) { //取下个ID的函数 var ret = db.counters.findAndModify( { query: { _id: name }, update: { $inc: { seq: 1 } }, //这里seq就是上面counters表中的seq字段 new: true, upsert: true } ); return ret.seq; }; > db.users.insert( //插入数据 { _id: getNextSequence("userid"), name: "tank" } ); WriteResult({ "nInserted" : 1 }) > db.users.find(); //查看 { "_id" : 1, "name" : "tank" } > db.users.insert( { _id: getNextSequence("userid"), name: "test" } ); WriteResult({ "nInserted" : 1 }) > db.users.find(); { "_id" : 1, "name" : "tank" } { "_id" : 2, "name" : "test" }
2,php实现auto_increment
function getNextId($mongo,$name,$param=array()){ $param += array( //默认ID从1开始,间隔是1 'init' => 1, 'step' => 1, ); $update = array('$inc'=>array('id'=>$param['step'])); //设置间隔 $query = array('name'=>$name); $command = array( 'findandmodify' => 'ids', 'update' => $update, 'query' => $query, 'new' => true ); $id = $mongo->db->command($command); if (isset($id['value']['id'])) { return $id['value']['id']; }else{ $mongo->insert(array( 'name' => $name, 'id' => $param['init'], //设置ID起始数值 )); return $param['init']; } } $mongo = new Mongo(); $curDB = $mongo->selectCollection('test', 'ids'); //test库中的ids表 $user = $mongo->selectCollection('test', 'users'); //test库中的users表 $id = getNextId($curDB,'userid',array('init'=>10000,'step'=>2)); //取得下一条数据的ID $obj = array("_id"=>$id,"name"=>"tankzhang"); $user->insert($obj); //插入数据
原文地址:mongodb php auto increment 自增, 感谢原作者分享。

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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.

In this chapter, we are going to learn the following topics related to routing ?

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

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

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