<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def dequeue_jobs(self, max_jobs=1, job_class=None, worker=None):
 """ Fetch a maximum of max_jobs from this queue """


        if job_class is None:
            from .job import Job
            job_class = Job

        count = 0



        # TODO: remove _id sort after full migration to datequeued
        sort_order = [("datequeued", -1 if self.is_reverse else 1), ("_id", -1 if self.is_reverse else 1)]

        # MongoDB optimization: with many jobs it's faster to fetch the IDs first and do the atomic update second
        # Some jobs may have been stolen by another worker in the meantime but it's a balance (should we over-fetch?)
        # job_ids = None
        # if max_jobs > 5:
        #     job_ids = [x["_id"] for x in self.collection.find(
        #         self.base_dequeue_query,
        #         limit=max_jobs,
        #         sort=sort_order,
        #         projection={"_id": 1}
        #     )]

        #     if len(job_ids) == 0:
        #         return

        for i in range(max_jobs):  # if job_ids is None else len(job_ids)):

            # if job_ids is not None:
            #     query = {
            #         "status": "queued",
            #         "_id": job_ids[i]
            #     }
            #     sort_order = None
            # else:
            query = self.base_dequeue_query

            job_data = self.collection.find_one_and_update(
                query,
                {"$set": {
                    "status": "started",
                    "datestarted": datetime.datetime.utcnow(),
                    "worker": worker.id if worker else None
                }, "$unset": {
                    "dateexpires": 1 # we don't want started jobs to expire unexpectedly
                }},
                sort=sort_order,
                return_document=ReturnDocument.AFTER,
                projection={
                    "_id": 1,
                    "path": 1,
                    "params": 1,
                    "status": 1,
                    "retry_count": 1,
                    "queue": 1,
                    "datequeued": 1
                }
            )

            if not job_data:
                break

            if worker:
                worker.status = "spawn"

            count += 1
            context.metric("queues.%s.dequeued" % job_data["queue"], 1)

            job = job_class(job_data["_id"], queue=self.id, start=False)
            job.set_data(job_data)
            job.datestarted = datetime.datetime.utcnow()

            context.metric("jobs.status.started")

            yield job

        context.metric("queues.all.dequeued", count)