Sequence concept implementation in mongo using pymongo & python


Sequence concept implementation in mongo using pymongo & python


import pymongo

# define the function that able to generate sequence number
def getNextSequence(seq_name):
    connection = pymongo.Connection("mongodb://localhost",
                                    safe=True)   

    db = connection.test
    sequence = db.sequences
    sequencenext = sequence.find_and_modify(query={'seq_name':seq_name},
                                       update={'$inc':{'sequence':1}},
                                       upsert=True, new=True

    sequenceno = sequencenext['sequence']
    return sequenceno



# Return sequence number for "Student" Table
print getNextSequence('student')
print getNextSequence('student')



You can use above concept same as sequence concept in oracle db and maintain the your own unique key for your own table.

You can use this generate sequence value instead of auto generation value for "_id" field of mongo document.

Share your use case for above implementation in mongo db.

thanks you and have nice weekend ..counting days for this year ending :).
 [Fri 6 Dec 2013][20:19:15]

Comments

Popular posts from this blog

Step by step guide on how to pay Income Tax that is due

Simple way producer–consumer problem's implementation in NodeJs

Rajiv Gandhi Equity Savings Scheme