Only get populate data on mongoose

0

I'm just trying to get data from a mongo query but I just need what the populate brings in an object, these are my models:

In this I have a collection of services (services):

var categoriesSchema = new Schema ({
    _id: { type: String, required: false },
    name: { type: String, required: true },
    sport_ids: { type: Array, required: false },
    product_categories: { type: Array, required: false },
    subcategories: { type: Array, required: false },
    family_name: { type: String, enum: [String , null], required: false },
    family_code: { type: String, enum: [String , null], required: false },
    youtube_channel: { type: String, required: false },
    services: [{ type: String, ref:'services', required: false }]
})

And this is the service:

var servicesSchema = new Schema ({
    _id: { type: String, required: true },
    name: { type: String, required: false },
    url : { type: String, required: false },
    is_generic: { type: Boolean, required: true }
}, { versionKey: false })

This is the function that brings the data:

exports.getServices = function (req, res) {
    Category.findOne({ _id : req.query.category_id }).select('-_id services')
        .populate('services')
        .exec(function (err, category) {
            if(err) res.send({status:500, message: 'internal error', type:'internal'});

            res.json(category)
            console.log('200    /getServices')
        })
}

Who delivers this data:

{
  "services": [
    {
      "_id": "financiacion",
      "name": "Financiación",
      "is_generic": true
    },
    {
      "_id": "sport-meeting",
      "name": "Sport meeting",
      "is_generic": false
    },
    {
      "_id": "bordado",
      "name": "Ventaja:  Bordado y estampación",
      "is_generic": false
    },
    {
      "_id": "disena-la-equipacion-de-tu-club",
      "name": "Diseña la equipación de tu club",
      "is_generic": true
    }
  ]
}

But actually I would like to get this:

       [{
          "_id": "financiacion",
          "name": "Financiación",
          "is_generic": true
        },
        {
          "_id": "sport-meeting",
          "name": "Sport meeting",
          "is_generic": false
        },
        {
          "_id": "bordado",
          "name": "Ventaja:  Bordado y estampación",
          "is_generic": false
        },
        {
          "_id": "disena-la-equipacion-de-tu-club",
          "name": "Diseña la equipación de tu club",
          "is_generic": true
        }]

Your help will be greatly appreciated!

    
asked by albertodente 24.08.2018 в 14:29
source

1 answer

0

Well, it was easier than I thought, to match an arrangement in the query, like this:

exports.getServices = function (req, res) {
    Category.findOne({ _id : req.query.category_id }).select('-_id services')
        .populate('services')
        .exec(function (err, category) {
            if(err) res.send({status:500, message: 'internal error', type:'internal'});
            servicesArray = category.services;
            res.json(servicesArray)
            console.log('200    /getServices')
        })
}

I hope someone serves you.

    
answered by 24.08.2018 в 15:50