1

I am using Nodejs backend with MongoDB to hold my object structures with Mongoose as the ORM.

I have a work order object which is following the below pattern. There are thousands of generated work orders and I have a requirement to find the work orders assigned to a given worker on a given date.

Could you help me to come up with a find query?

The query details

  1. Should be able to query and find itenary - allocated work order - details for a given worker in a given date
  2. Need to use allocatedWorker (worker) and allocatedDateTime (date and time) to match and find

Work order object

{
    customer:       String,
    address:        String,
    /**
     * List of to do items. Each item will mainly belong in to a given category
     * and a collection of tasks.
     * The task can be system generated, admin assigned or customer raised.
     */
    todo: [
        {
            category: String,

            /**
             * A collection of raised tasks
             */
            tasks: [
                {
                    /**
                     * Holds the customer request details
                     */
                    request: {
                        workItem:           String,
                        instructions:       String,

                        /**
                         * Preferred date and time
                         */
                        dateTime:           String,
                        /**
                         * Preferred worker if any
                         */
                        preferredWorker:    String,
                        receivedDateTime:   String
                    },

                    /**
                     * Details of the schedule
                     */
                    schedule: [
                        {
                            /**
                             * Allocated worker's user name
                             */
                            allocatedWorker:    String,
                            allocatedDateTime:  String,
                            allocatedDuration:  Number,
                            /**
                             * Schedule changed date and time
                             */
                            scheduledOn:        String,
                            /**
                             * Scheduler's user name
                             */
                            scheduledBy:        String,
                            /**
                             * Scheduled task status
                             *  0: Pending - new
                             *  1: Scheduled - allocated
                             *  2: Work In Progress
                             *  3: Completed
                             *  4: Overdue
                             */
                            itemStatus:         Number
                        }
                    ],

                    /**
                     * Stage of the work order
                     *  0: Pending - new
                     *  1: Scheduled - allocated
                     *  2: Work In Progress
                     *  3: Completed
                     *  4: Overdue
                     */
                    status:                 Number
                }
            ]
        }
    ]
}
1
  • This is what I have tried, Workorder.find({$and:[{'todo.tasks.schedule.allocatedWorker': worker}, {'todo.tasks.schedule.allocatedDateTime': day}]}, function(err, orders) {}) Commented Sep 30, 2016 at 0:10

1 Answer 1

3

Try below query:

Workorder.find({'todo.tasks.schedule.allocatedWorker'‌​: worker, 
'todo.tasks.schedule.allocatedDateTime': day}, function(err, orders) {});

Remember that both will do exact value match(exact name and dateTime) stored in db.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.