How to set primary key with auto increment in MongoDB? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-05 10:07:18

问题


How to set primary key with auto increment in MongoDB ?

Normally MongoDB generates ObjectID. I wanna use my own auto increment primary key like MySQL.

Examples would be appreciated :)


回答1:


I blogged about this here:
http://www.alexjamesbrown.com/blog/development/mongodb-incremental-ids/

I also started a int id generator for the C# driver:
https://github.com/alexjamesbrown/MongDBIntIdGenerator

However, incrementing ID's won't scale effectively.




回答2:


$db->execute("return db.getCollection('autoInccollection').findAndModify({
                        query: { collection: '$colle' },
                        update: { '$inc': { primaryKey: 1 } },new: true
                            })");

$colle means which collection you need to create the Auto increment key.



来源:https://stackoverflow.com/questions/18334279/how-to-set-primary-key-with-auto-increment-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!