laravel 如何创建自增字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9921904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create auto increment field
提问by Rohan Kumar
I want to add auto increment value in a field in mongodb using php, without using counting the records and next number(count + 1) will be my increment value...
Or Is there any way to create array of autoincrement values in mongodb
,php
for example A customer having multiple emails and addresses then we can store it like email[{1:'[email protected]',2:'[email protected]'},...]
and also same for address[{1:'Indore',2:'Dewas'},...]
So for uniqueness I want to set autoincrement value from each email and address..
我想使用 php 在 mongodb 的字段中添加自动增量值,而不使用记录计数,下一个数字(计数 + 1)将是我的增量值......或者有什么方法可以在 中创建自动增量值数组mongodb
,php
对于例如,一个客户有多个电子邮件和地址,那么我们可以像这样存储它email[{1:'[email protected]',2:'[email protected]'},...]
,也可以存储它,address[{1:'Indore',2:'Dewas'},...]
所以为了唯一性,我想从每个电子邮件和地址设置自动增量值。
回答by Rohan Kumar
It is possible if you create two collections
如果您创建两个集合是可能的
Firstto store the field for whichyou want to create autoincrement numberlike
首先存储要为其创建自动增量编号的字段,例如
// Inserting 2 records for which we want to create auto-increment numbers
db.autoincrementcollection.insert({
field_id: "register_id",
nextId: 0
},
{
field_id: "user_id",
nextId: 0
});
// Function is used to get the next autoincremented value
function getNextId(field)
{
var ret = db.autoincrementcollection.findAndModify({
query: { field_id: field },
update: { $inc: { nextId: 1 } },
new: true
});
return ret.nextId;
}
Secondin which you want to use the incremented number like
第二个你想使用递增的数字,如
// inserting data in registration collection with register_id auto-increment number
db.registration.insert({
_id: getNextId("register_id"),
fname: "Rohan",
lname:"Kumar"
});
// inserting data in user collection with user_id auto-increment number
db.users.insert({
_id: getNextId("user_id"),
name: "Rohan Kumar"
});
findAndModify()may not work while code using PHPthen you can use find()and/or update()
findAndModify()在使用PHP 编写代码时可能不起作用,然后您可以使用find()和/或update()
Refer Docsfor create-an-auto-incrementing-field
请参阅文档以创建自动递增字段
回答by kunal
It is possible we have to get max id form collection
有可能我们必须获得最大 id 表单集合
db -> collection -> find() -> sort(array('id' => -1)) -> limit(1)
It return max id array. Add db['id'] + 1;
它返回最大 id 数组。添加db['id'] + 1;
Every time need not store the auto-increment id in mongo
不需要每次都在 mongo 中存储自动递增的 id
回答by DhruvPathak
Ideally you should not be using autoincrement as identifier in MongoDb. If you want to , refer 2 good methods given here :
理想情况下,您不应该在 MongoDb 中使用自动增量作为标识符。如果您愿意,请参考此处给出的 2 种好方法:
http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field
http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field
回答by Payam Khaninejad
This is my method for creating auto increment id in MongoDB PHP library:
这是我在 MongoDB PHP 库中创建自动增量 ID 的方法:
private function get_last_id()
{
// put your connection string blow
$client = new MongoDB\Client('');
$counterCollection = $client->collection->counter;
$counter["id"]=1;
if ($counterCollection->count()<=0) {
$insertOneResult = $counterCollection->insertOne($counter);
} else {
$filter = [];
$options = ['sort' => ['_id' => -1], 'limit' => 1];
$doc = $counterCollection->findOne($filter, $options);
$id=$doc->id+1;
$updateResult = $counterCollection->updateOne(
[ '_id' => $doc->_id ],
[ '$set' => [ 'id' =>$id ]]
);
return $id;
}
}