简体   繁体   中英

Get data from another collection using MongoDB

Using MongoDB I am getting the sales data where I am getting the customer of the sale but I am only getting their "_id" but I need to get their data like the name. It's possible? This is my query made to my sales collection where the customer is the one to whom the sale was made:

db.sales.aggregate([
  {
    $group: {
      _id: {
        month: {
          $month: "$createdAt"
        },
        year: {
          $year: "$createdAt"
        },
        dayOfWeek: {
            $dayOfWeek: "$createdAt"
        },
        stringDay: { 
            $dateToString: 
            { format: "%Y-%m-%d", date: "$createdAt"}
        },
        week: {
            $isoWeek: "$createdAt"
        }
      },
      paymentType: { $first: '$paymentType' },
      client: { $first: '$clientId' },
      total: { $sum: '$total'},
      count: { $sum: 1 },
      totalAverage: { $avg: '$total'},
    }
  },
  {
    $project: {
      total:  { $round: [ "$total", 2 ] },
      year: "$_id.year",
      date: "$_id.date",
      week: "$_id.week",
      numVentas: "$count",
      month: "$_id.month",
      dayOfWeek: "$_id.dayOfWeek",
      stringDay:"$_id.stringDay",
      count: "$count",
      paymentType: "$paymentType",
      client: "$client",
      totalAverage: { $round: [ "$totalAverage", 2 ] },
      stringMonth: {
        $arrayElemAt: [
          [
            "",
            "Jan",
            "Feb",
            "Mar",
            "Apr",
            "May",
            "Jun",
            "Jul",
            "Aug",
            "Sep",
            "Oct",
            "Nov",
            "Dec"
          ],
          "$_id.month"
        ]
      },
      stringWeek: {
        $switch: {
            branches:[
                { case: { $eq: ["$_id.dayOfWeek", 1] }, then: "Lunes" },
                { case: { $eq: ["$_id.dayOfWeek", 2] }, then: "Martes" },
                { case: { $eq: ["$_id.dayOfWeek", 3] }, then: "Miércoles" },
                { case: { $eq: ["$_id.dayOfWeek", 4] }, then: "Jueves" },
                { case: { $eq: ["$_id.dayOfWeek", 5] }, then: "Viernes" },
                { case: { $eq: ["$_id.dayOfWeek", 6] }, then: "Sábado" },
                { case: { $eq: ["$_id.dayOfWeek", 7] }, then: "Domingo" }
            ],
            default: "Día desconocido"
            }
        }
    }
  },
  {
    $group: {
        _id:  { month: "$month", stringMonth: "$stringMonth", year: "$year"},
        count: { $sum: "$count" },
        total: { $sum: "$total" },
        totalAverage: { $sum: "$totalAverage" },
        sales: {
            $push: { 
                client: "$client",
                paymentType: "$paymentType",
                numberDay: "$dayOfWeek",
                week: "$week",
                stringWeek: "$stringWeek",
                date: "$stringDay",
                total: "$total",
                count: "$count",
                totalAverage: { $round: [ "$totalAverage", 2 ] }  
            }
        }
    }
  },
  {
    $group: {
        _id: "$_id.year",
        monthsWithSales: { $sum: 1 },
        count: { $sum: "$count" },
        total: { $sum: "$total" },
        totalAverage: { $sum: "$totalAverage" },
        sales: {
            $push: {
                month: "$_id.month",
                stringMonth: "$_id.stringMonth",
                count: "$count",
                total: "$total",
                totalAverage: "$totalAverage",
                sales:"$sales"
            }
        }
    }
  }
  
])


And this brings me only the "_id" of the client of the sale but I also need to obtain other data such as the client name that is in the clients collection:


    "_id": 2022,
    "monthsWithSales": 4,
    "count": 57,
    "total": 22324.8,
    "totalAverage": 7765.799999999999,
    "sales": [
      {
        "month": 12,
        "stringMonth": "Dec",
        "count": 33,
        "total": 12587.6,
        "totalAverage": 4074.5,
        "sales": [
          {
            **"client": {
              "$oid": "63890f616d8780e8608266f5"
            },**
            "paymentType": "CASH",

In MongoDB aggregation you can use Lookup operator for this. refer:- Read about Lookup on MongoDB website.

If you are using Mongoose You can use Populate method too.

Both these takes docs from another collection if they have a common field and adds into current collection. It can be complicated to discuss here. So please refer the official documentation for Lookup or populate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM