我想在使用php的MongoDB中的字段中添加自动增量值,而不使用计数记录,下一个数字(count + 1)将作为我的增量值... 或者是否有任何在mongodb
,php
例如一个客户有多个电子邮件和地址,然后我们可以存储它像email[{1:'[email protected]',2:'[email protected]'},...]
和address[{1:'Indore',2:'Dewas'},...]
也相同因此为了唯一性,我想设置自动增量值从每个电子邮件和地址..如何创建自动增量字段
1
A
回答
2
这是可能的,如果你创建了两个collections
首先存储领域whichyou要创建自动增量数像
// 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;
}
0
理想情况下,您不应该在MongoDb中使用自动增量作为标识符。 如果你想,参考2种好方法在这里给出:
http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field
0
使用递增的编号类似
// 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()可能无法正常工作
这是可能的,我们必须得到最大id表格集合
db - > collection - > find() - > sort(array('id'=> -1)) - > limit(1)
它返回最大id数组。添加db ['id'] + 1;
每次都不需要存储mongo中的自动增量id
相关问题
- 1. 如何创建自动增量主键?
- 2. 自动增量字段
- 3. 在Grails&Oracle中创建一个自动增量(non id)字段
- 4. 在Spark SQL临时表中创建自动增量字段
- 5. CakePHP:如何获取最近创建的自动递增字段
- 6. 如何在轨道中创建INT字段,以10001开始自动增量?
- 7. 了解自动增量字段的值
- 8. IBM Worklight JSONStore自动增量字段
- 9. SharePoint 2010自动增量字段
- 10. 插入一个自动增量字段
- 11. 如何创建自动增量变量Odoo POS
- 12. 创建自动增量与前文
- 13. 如何获取自动递增字段
- 14. 如何自动增加Id字段
- 15. 如何创建只包含更改字段的增量对象?
- 16. Rails&Postgresql创建一个字段,自动递增1
- 17. 如何使在Django的自动增量字段从10000开始
- 18. 如何在nedb中使用自动增量字段?
- 19. 如何在MySQL中回填自动增量字段
- 20. 如何重置ActiveRecord迁移中的自动增量字段?
- 21. MySQL:如何设置自动增量字段的起始值?
- 22. 如何在DynamoDB中设置自动增量字段?
- 23. 如何使用JPA批注注释MYSQL自动增量字段
- 24. 如何使用batch for loop创建自动增量文件?
- 25. 如何在SQLite中创建自动增量主键
- 26. 如何创建另一个MySQL自动增量列?
- 27. 自动递增字段不
- 28. 如何在莲花多米诺骨牌中创建自动递增字段?
- 29. 如何自动增量(IDENTITY)
- 30. 如何自动增量ID
你怎么用PHP实现上面的? – Slimshadddyyy 2014-09-26 04:09:32