3
3
32012612021-07-06 17:55:08
MongoDB
3201261, 2021-07-06 17:55:08

How to query nested array with Mongoose?

There is a model

var RequestSchema = new Schema({
    requestNumber: {
        type: String
    },
    requestDescription: {
        type: String,
        required: [true, 'Необходимо добавить описание']
    },
    orders: [{
        product: {
            type: Schema.Types.ObjectId,
            ref: 'Product',
            required: true
        },
        finishTo: {
            type: Date,
            required: true
        }
    }],
    createdAt: {
        default: Date.now,
        type: Date
    }
});


I want to sort orders by the nearest delivery date - finishTo, when sorting from smallest to largest, {"orders.finishTo":1} is sorted as needed, and if sorted in descending order, the maximum finishTo in the order is taken.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
3
3201261, 2021-07-07
​​@3201261

The issue is resolved, there was version 3.2 in which there was no aggregate. After the upgrade, mongodb rewrote the query using aggregate.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question