Thursday, 8 June 2017

How can I get sum of a field value from another collection in mongodb

I have two collection, one is trip and another is shipment. One trip can have multiple shipments.

Shipment schema :

_id: {
        type: String,
        required: true
    },
    trip_id: {
        type: String,
        required: true
    },
    traveller_email_id: {
        type: String,
        required: true
    },
    email_id: {
        type: String,
        required: true
    },
    item_name: {
        type: String,
        default: true
    },
    description_of_shipment: {
        type: String,
        default: ''
    },
    required_weight: {
        type: Number,
        required: true
    },
    required_price: {
        type: Number,
        required: true
    },
    pickup_needed: {
        type: String,
        enum: ['No', 'Yes'],
        default: 'No',
        required: true
    },
    pickup_address: {
        type: String,
        default: ''
    },
    recipient_name: {
        type: String,
        required: true
    },
    recipient_place_name: {
        type: String,
        required: true
    },
    recipient_address: {
        type: String,
        required: true
    },
    recipient_contact_number: {
        type: String,
        required: true
    },
    order_number: {
        type: String,
        default: ''
    },
    accepted_status: {
        type: String,
        enum: ['Declined', 'Accepted', 'Requested','Transit','Pending','Delivered'],
        default: 'Requested',
        required: true
    }
}

I want to get sum of "required_weight" for every trip. I am joining the trip table with shipment table by trip_id foreign key.



via Tina

No comments:

Post a Comment