G
G
gsdev992019-06-07 18:59:27
MongoDB
gsdev99, 2019-06-07 18:59:27

How to read the request correctly?

Hello. Guys, tell me, please, how to read the request correctly?
I have a collection: Products

/* 1 */
{
    "_id" : ObjectId("5b2d3648ca6a03cd33af924c"),
    "sold" : 38,
    "name" : "AZ2040",
    "description" : "Super awesome guitar",
    "price" : 2000,
    "brand" : ObjectId("5b2c11d2d37177aedfd6d962"),
    "shipping" : true,
    "available" : true,
    "wood" : ObjectId("5b2c1c88255983b4795f8fdb"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T17:47:52.838Z"),
    "updatedAt" : ISODate("2018-06-26T22:09:18.969Z"),
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("5b2d38027d75e2cdcb31cf04"),
    "sold" : 4,
    "name" : "Roadcore",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 1500,
    "brand" : ObjectId("5b2c11d2d37177aedfd6d962"),
    "shipping" : true,
    "available" : true,
    "wood" : ObjectId("5b2c1c0981e781b44909627d"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T17:55:14.152Z"),
    "updatedAt" : ISODate("2018-06-26T22:09:18.972Z"),
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("5b2d38217d75e2cdcb31cf05"),
    "sold" : 15,
    "name" : "JEM UV7",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 3500,
    "brand" : ObjectId("5b2c11d2d37177aedfd6d962"),
    "shipping" : true,
    "available" : false,
    "wood" : ObjectId("5b2c1c81255983b4795f8fda"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T17:55:45.880Z"),
    "updatedAt" : ISODate("2018-06-22T17:55:45.880Z"),
    "__v" : 0
}

/* 4 */
{
    "_id" : ObjectId("5b2d384b7d75e2cdcb31cf06"),
    "sold" : 0,
    "name" : "TELE-ub23",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 600,
    "brand" : ObjectId("5b2c11481470fbaecd159dfd"),
    "shipping" : false,
    "available" : false,
    "wood" : ObjectId("5b2c1c0981e781b44909627d"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T17:56:27.270Z"),
    "updatedAt" : ISODate("2018-06-22T17:56:27.270Z"),
    "__v" : 0
}

/* 5 */
{
    "_id" : ObjectId("5b2d390d7d75e2cdcb31cf07"),
    "sold" : 9,
    "name" : "FR7",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 1300,
    "brand" : ObjectId("5b2c12a4d37177aedfd6d967"),
    "shipping" : false,
    "available" : true,
    "wood" : ObjectId("5b2c1c81255983b4795f8fda"),
    "frets" : 24,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T17:59:41.868Z"),
    "updatedAt" : ISODate("2018-06-22T17:59:41.868Z"),
    "__v" : 0
}

/* 6 */
{
    "_id" : ObjectId("5b2d39407d75e2cdcb31cf08"),
    "sold" : 0,
    "name" : "Blue6",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 700,
    "brand" : ObjectId("5b2c12a1d37177aedfd6d966"),
    "shipping" : true,
    "available" : true,
    "wood" : ObjectId("5b2c1c81255983b4795f8fda"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T18:00:32.803Z"),
    "updatedAt" : ISODate("2018-06-22T18:00:32.803Z"),
    "__v" : 0
}

/* 7 */
{
    "_id" : ObjectId("5b2d39697d75e2cdcb31cf09"),
    "sold" : 4,
    "name" : "PET967",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 1200,
    "brand" : ObjectId("5b2c171c357cb8b03cb04342"),
    "shipping" : true,
    "available" : true,
    "wood" : ObjectId("5b2c1c0981e781b44909627d"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T18:01:13.043Z"),
    "updatedAt" : ISODate("2018-06-22T18:01:13.043Z"),
    "__v" : 0
}

/* 8 */
{
    "_id" : ObjectId("5b2d39987d75e2cdcb31cf0a"),
    "sold" : 0,
    "name" : "Robot 5",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 1200,
    "brand" : ObjectId("5b2c1267d37177aedfd6d965"),
    "shipping" : true,
    "available" : true,
    "wood" : ObjectId("5b2c1c0981e781b44909627d"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T18:02:00.569Z"),
    "updatedAt" : ISODate("2018-06-22T18:02:00.569Z"),
    "__v" : 0
}

/* 9 */
{
    "_id" : ObjectId("5b2d39bd7d75e2cdcb31cf0b"),
    "sold" : 3,
    "name" : "Dinky 7",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 600,
    "brand" : ObjectId("5b2c1264d37177aedfd6d964"),
    "shipping" : true,
    "available" : true,
    "wood" : ObjectId("5b2c1c88255983b4795f8fdb"),
    "frets" : 24,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T18:02:37.344Z"),
    "updatedAt" : ISODate("2018-06-26T06:11:52.923Z"),
    "__v" : 0
}

/* 10 */
{
    "_id" : ObjectId("5b2d39d97d75e2cdcb31cf0c"),
    "sold" : 15,
    "name" : "RoadCaster",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 1,
    "brand" : ObjectId("5b2c11481470fbaecd159dfd"),
    "shipping" : true,
    "available" : false,
    "wood" : ObjectId("5b2c1c0981e781b44909627d"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T18:03:05.168Z"),
    "updatedAt" : ISODate("2018-06-26T23:32:52.944Z"),
    "__v" : 0
}

/* 11 */
{
    "_id" : ObjectId("5b2d4b70e4b4a1d22d374f8b"),
    "sold" : 12,
    "name" : "ACU700",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ",
    "price" : 1,
    "brand" : ObjectId("5b2c1216d37177aedfd6d963"),
    "shipping" : true,
    "available" : true,
    "wood" : ObjectId("5b2c1c81255983b4795f8fda"),
    "frets" : 22,
    "publish" : true,
    "createdAt" : ISODate("2018-06-22T19:18:08.458Z"),
    "updatedAt" : ISODate("2018-06-26T23:32:52.949Z"),
    "__v" : 0
}

The following request is written in the server.js file:
// http://localhost:3002/api/product/articles/?sortBy=createdAt&order=desc&limit=4
app.get('/api/product/articles', (req, res) => {
    let order = req.query.order ? req.query.order : 'asc';

    let sortBy = req.query.sortBy ? req.query.sortBy : '_id';

    let limit = req.query.limit ? parseInt(req.query.limit) : 100;

    console.log('order:', order); // desc
    console.log('sortBy:', sortBy); // createdAt
    console.log('limit:', limit); // 4

    Product.find()
        .populate('brand')
        .populate('wood')
        .sort()
        .limit(limit)
        .exec((err, articles) => {
            if (err) {
                return res.status(400).send(err);
            }
            res.send(articles);
        });
});

Actually a question:
How correctly there is a sequence of request?
In the products collection we are looking for a document:
(not clear: .populate('brand') .populate('wood').sort())
And what does exec mean?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question