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