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 :)
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 :)
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.
$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.