0

I have this schema in mongoose:

var schema = new Schema({
name: {type: String, required: true},
description: {type: String, required: true},
subcategory: {type: Schema.Types.ObjectId, ref: 'SubCategory'},
price: [{type: Schema.Types.ObjectId, ref: 'Price'}],
provider: [{type: Schema.Types.ObjectId, ref: 'Provider'}]
});

var schema = new Schema({
monto: {type: Float, required: true},
fecha: {type: Date, required: true},
idProvider: {type: Schema.Types.ObjectId, ref: 'Provider'},
idProduct: {type: Schema.Types.ObjectId, ref: 'Product'}
});

I need getall products populated and I need to obtain the current price of each product and show only that. I tried doing this but doesn't work:

router.get('/', (req, res, next) => {
Product.find({})
.populate({path: 'price', options: { $sort: {'fecha': -1}, limit: 1}})
.then(product => {
    if(!product) {return res.sendStatus(401);}
    return res.json(product)
})
.catch(next);
})

For example:

{
  "_id": "5a4eebdd24b0412d0cf58601",
  "name": "Heladera",
  "description": "Automatico con centrifugado",
  "__v": 1,
  "provider": [],
  "price": [
     {
       "_id": "5a4ea416e723b42c28fd624e",
       "monto": 8560.23,
       "fecha": "2000-12-12T03:00:00.000Z",
       "idProduct": "5a4ea3b3e723b42c28fd624d",
       "__v": 0
      }
      {
       "_id": "5a4f0741f722c01528582104",
       "monto": 900,
       "fecha": "2018-12-12T03:00:00.000Z",
       "idProduct": "5a4f072ff722c01528582103",
       "__v": 0
        }
    ]
}

I need to show

{
    "_id": "5a4eebdd24b0412d0cf58601",
    "name": "Heladera",
    "description": "Automatico con centrifugado",
    "__v": 1,
    "provider": [],
    "price": [
        { monto: 900}
     ]
 }
1
  • What is not working ? Does populate work by itself ? Commented Jan 5, 2018 at 11:34

1 Answer 1

1

You might write an aggregation pipeline like this.

db.products.aggregate([
  { $unwind: "$price" },
  { $sort: { "_id": 1, "price.fecha": -1 } },
  { $group: { _id: "$_id", product: { $first: "$$ROOT" } } }
]);

This gives you the following document.

{
    "_id" : "5a4eebdd24b0412d0cf58601",
    "product" : {
        "_id" : "5a4eebdd24b0412d0cf58601",
        "name" : "Heladera",
        "description" : "Automatico con centrifugado",
        "__v" : 1.0,
        "provider" : [],
        "price" : {
            "_id" : "5a4f0741f722c01528582104",
            "monto" : 900.0,
            "fecha" : "2018-12-12T03:00:00.000Z",
            "idProduct" : "5a4f072ff722c01528582103",
            "__v" : 0.0
        }
    }
}

In case you need only the price information, you can add a project stage to reshape the returned document.

For instance, adding this final stage to the above pipeline

{ $project: { 
    _id: 1, 
    name: "$product.name", 
    description: "$product.description", 
    __v: "$product.__v", 
    "monto": "$product.price.monto" 
}}

the document is reshaped as follows.

{
    "_id" : "5a4eebdd24b0412d0cf58601",
    "name" : "Heladera",
    "description" : "Automatico con centrifugado",
    "__v" : 1.0,
    "monto" : 900.0
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.