0

This is an example of a data document

{
    "_id" : ObjectId("5f437e7846103b2ad0fc5d7d"),
    "order_no" : "O-200824-AGFJDQW",
    "shipment_no" : "S-200824-AGWCRRM",
    "member_id" : 2200140,
    "ponta_id" : "9990010100280214",
    "plu" : 14723,
    "product_name" : "AQUA Air Mineral Botol Air Pet 600ml",
    "qty" : 2,
    "store_id" : "TD46",
    "stock_on_hand" : 0,
    "transaction_date" : ISODate("2020-08-24T08:28:29.931Z"),
    "created_date" : ISODate("2020-08-24T08:46:48.441Z")
}

this is the data query that I run

var bulan = 12 //month is written with number. example: August = 8
db.log_stock_oos.aggregate([
  {
      $project: {
          month: {
              $month: '$transaction_date'
              }
          }
  },
  {
      $match: {month: bulan}
  }
]);

but the result is like this after I run the query

{
    "_id" : ObjectId("5f44689607fe453fbfba433e"),
    "month" : 12
}

how to make the output exactly like the document display that I attached above?? this is my reference

hirarqi
  • 251
  • 2
  • 14

1 Answers1

1

When you use the projection, its kind of if your value 1 then include the field, if your value 0 then exclude the field from the whole documents. Projection

You can do two things

Use the projection

db.collection.aggregate([
  {
    $project: {
      month: {
        $month: "$transaction_date"
      },
      order_no: 1,
      shipment_no: 1,
      member_id: 1,
      //other fields like above with the value 1
    }
  },
  // match stages
])

Use $addFields

use $addFields incited of $project in your code. If will create a filed if not exists in your document, else it will overwrite the field

varman
  • 8,704
  • 5
  • 19
  • 53