Issue whille sorting by ulid

in my python app i have a function that generated random codes:

def gen_random_codes(count, username, product_name=None, batch_id=None):
    return [
        {
            "_id": ulid.new().str,
            "owner_name": username,
            "generated_on": datetime.now(timezone.utc),
            "code": code_gen(8),
            "product": product_name,
            "batch_id": batch_id,
            "status": False,
        }
        for _ in range(count)
    ]

Then i insert them using this:db.codes.insert_many(generated_codes)
after this i insert the batch information in another collection:

db.batch_request.insert_one(
        {
            "_id": ulid.new().str,
            "email": user_email,
            "start": start_series,
            "end": end_series,
            "count": total_count,
            "timestamp": datetime.now(timezone.utc)}
    )

start series and end series are retrieved using start_series=codes[0]["_id"], end_series=codes[-1]["_id"] now in another route i want to get all the codelist associated with that batch using the starting ulid and ending ulid: @user.route('/download-batch', methods=["GET", "POST"])@login_requireddef do - Pastebin.com and here is the function:

def get_code_list_by_series(start, end):
    return list(db.codes.find({"owner_name": current_user.id, "$and": [{"_id": {"$gte": start}}, {"_id": {"$lte": end}}]}))

i am usning $gte and $lte to fetch all the codes generated for that batch. But the problem is it is not returning the correct number of codes. eg. if 100 codes are inserted it’s returning probably 60-70 codes. Can anyone help me what i am doing wrong. Because as per my knowledge we can sort the ulid lexiographically

here is the db schema of db.codes: Batches--------------_id:"01GTFAJM8HNE96BSS8TQ9AENMW"email:"xyz@gmail.com" - Pastebin.com

Hi @BIJAY_NAYAK welcome to the community!

the problem is it is not returning the correct number of codes. eg. if 100 codes are inserted it’s returning probably 60-70 codes.

Are you sure the query is supposed to return 100 codes instead of only 60-70 of them? I don’t see anything wrong with the query itself (it’s just a range query on _id), so if the query looks right, the only explanation is that the data is not correct.

as per my knowledge we can sort the ulid lexiographically

Perhaps this is the issue here. Maybe ULID doesn’t behave like you think it does?

One quick way to test your assumptions is to replace this line:

"_id": ulid.new().str,

with something else that you know will definitely work as expected. Maybe a sequential integer? This way, if your query does return the expected number, we can definitely say that the ULID generator doesn’t behave like you expect.

Best regards
Kevin

1 Like