Thursday, 18 May 2017

Search mongodb for items belonging to a certain user that fall within a date range

I'm trying to query my mongodb for items that were created between two dates and that belong to the "signed in user". I have created most of the code that works but without a "user_id" field. When I add that to the code below, I get an error:

    let matchCrit = {
        //when I console.log() the code below, it logs the correct user id
        "customer_id": request.auth.credentials.account_id,
        "date_created": {
            $gte: new Date(request.payload.from),
            $lte: new Date(request.payload.to)
        }
    };

    db_mgr.find("ds_shipment_records", matchCrit).then((serviceItems) => {

        serviceItems.forEach((serviceItem) => {
            serviceItem.status.text = resourceSet.shipment.status_text[serviceItem.status.code];
        });

        return reply({
            "success": true,
            "message": "Service item record list",
            "csrf-decorator": csrf_decorator,
            "service_record_list": serviceItems || [],
            "redirect": ""
        }).type("application/json");
    }).catch((err) => {
        return reply({"success": false, "message": err.message, "csrf-decorator": csrf_decorator, "redirect": ""}).type("application/json").code(400);
    }).done();

Hope that makes sense. If not I'm happy to clarify. I think the error might be how I'm using the matchCrit. I'm not sure if this is how I should call for the params in the DB.

Thanks in advance!



via Atlante Avila

No comments:

Post a Comment