logo
Tags down

shadow

Populate field in aggregation mongodb


By : Rehan Khan
Date : October 15 2020, 08:10 PM
should help you out I am using below aggregation query to get data from two collections: , You can use below aggregation
code :
Project.aggregate([
  { "$match": condition },
  { "$group":{ "_id": "$_id" }},
  { "$lookup": {
    "from": "worksheets",
    "let": { "projectId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$projectId", "$$projectId" ] }}},
      { "$group": {_id:"$projectId", totalHours:{"$sum": "$hours"}}},
      { "$lookup": {
        "from": "projects",
        "let": { "projectId": "$_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$_id", "$$projectId" ] }}},
          { "$project": { "projectName": 1 }}
        ],
        "as": "project"
      }}
    ],
    "as": "worksheet"
  }}
])


Share : facebook icon twitter icon

How to use populate functionality by using populate or making inner query with aggregation in mongodb


By : user2979512
Date : March 29 2020, 07:55 AM
To fix this issue This is working like applied populate with aggregate using inner query.
code :
   var mongoose = require("mongoose");
    var empid = mongoose.Types.ObjectId("54a0d4c5bffabd6a179834eb");
    Availability.aggregate() 
        .match( { employee_id : empid} )
        .group({_id : "$employee_id",count: { $sum: 1 }})
        .exec(function (err, response) {
            if (err) console.log(err);
            if (response.length) {
                     var x = 0;
                    for (var i=0; i< response.length; i++) {
                          empID = response[i]._id;
                          if (x === response.length -1 ) {
                          User.find({_id: empID}, function(err, users){
                            res.json({"message": "success", "data": users, "status_code": "200"});
                          });
                          }
                          x++;
                    }
            }
        }
    );

Mongodb: Is it possible to create ISODate field in Aggregation Pipeline from an existing string field?


By : sk anand
Date : March 29 2020, 07:55 AM
seems to work fine In honour of receiving the much coveted "Tumbleweed" badge, having received no responses, I thought I'd post what I landed up doing in case anyone else stumbles into this same issue...
As it seemed impossible to cast a string field on to date field in the Aggregation pipeline, I did the work in php:-
code :
$greatestAcceptableDate = new MongoDate(strtotime( "-14 days"));

$cursor = db.getCollection('updates').find(array("summary_display_tags.Source"=> "whatever"));
foreach ($cursor as $rec){   
   $recordTS = $this->buildTimestamp(strtotime(str_replace('-','/',substr($rec['date_modified'],0,10)).' 00:00:00'));
   if ($recordTS < $greatestAcceptableDate){
   db.getCollection('updates').remove("_id": $rec('_id'));
}

MongoDB Aggregation: $Project (how to use a field on the other field of the same projection pipeline)


By : Gareth Cadwaladr
Date : March 29 2020, 07:55 AM
it helps some times So i guess there is no way I can use fields on other fields that co-exist on same projection pipeline.
(assume the settled_bills and overdue_bills consist not just the 'size' but with long query operators )
code :
db.Collection.aggregate([
{
   $project: {
      all_bills: ‘$all_count’,
      settled_bills: { $size: ’$settled’ },
      overdue_bills: { $size: ‘$overdue’ },
   },
   $project: {
      settled_percentage: {
          $divide : ['$settled_bills','$overdue_bills']   
       }
   }
}
])

Add field to documents after $sort aggregation pipeline which include its index in sorted list using MongoDb aggregation


By : sunrise3333
Date : March 29 2020, 07:55 AM
Any of those help One of the workaround for this situation is to convert your all documents into one single array and hence resolve the index of the document using this array with help of $unwind and finally project the data with fields as required.
code :
db.collection.aggregate([
  { $sort: { points: 1 } },
  {
    $group: {
      _id: 1,
      register: { $push: { _id: "$_id", name: "$name", points: "$points" } }
    }
  },
  { $unwind: { path: "$register", includeArrayIndex: "order" } },
  { $match: { "register.name": "x4" } },
  {
    $project: {
      _id: "$register._id",
      name: "$register.name",
      points: "$register.points",
      order: 1
    }
  }
]);

MongoDB Aggregation: add field from an embedded document via a dynamic field path


By : vicky
Date : March 29 2020, 07:55 AM
This might help you I have an aggregation pipeline and am stuck on a stage. At this point I have a document in the following format: , Use this one:
code :
db.collection.aggregate([
   { $set: { data: { $objectToArray: "$levelNames" } } },
   {
      $set: {
         currentLevelId: {
            $arrayElemAt: ["$data.v", { $indexOfArray: ["$data.k", "$levelName"] }]
         }
      }
   },
   { $unset: "data" }
])
shadow
Privacy Policy - Terms - Contact Us © voile276.org