Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

aggregate mongodb

// MonogDB Aggregation Framework
// $project -> $match -> $group -> $sort -> ouput
// $count, $skip, Sunwind,
// $match
db.users.aggregate([
{ stage 1}, $project
 { stage 2}, $match
  { stage 3}, $group
   { stage 4}, $sort
]);

* match : 
db.users.aggregate([{$match:{'status':'inactive'}}]).pretty();

* group :
// group by department 
db.users.aggregate([{$group:{_id:'$department'}}]).pretty();
{"id" : "IT" } {
{"_id" : "TECH" }
{_"id" : "HR" }

- multiple group 
db.users.aggregate([{$group:{_id:{age:'Şage',gender:'$gender'}}}]).pretty();

{"id" : { "age" : "27", 'gender' : "female" }}
{"_id" : { "age" : "25", "gender" : "female" } }
{ "_id" : { "age" : "26" "gender" : "male" } }
{ "_id" : { "age" : "24", "gender" : "male" } }
{ "_id" : { "age" : "22", "gender" : "male" } }
{"_id" : { "age" : "27", "gender" : "male" } }

* sort 
db.users.aggregate([{$group:{_id:'$department'}},{$sort:{_id:1}}]);

* match 
db.users.aggregate([{$match:{status:'active'}},{$project:{name:1,email:1}}]).pretty();

* count 
db.users.aggregate([{$match:{status:'inactive'}},{$count:'totals'}]);

* limit 
db.users.aggregate([{$match:{status:'active'}},{$project:{name:1,email:1}},{Slimit:2}]).pretty();

* unwind 
- It is used for array attribute to show seprate result for that array.

db.users.aggregate([{$unwind:"$language"},{$match:{status:'inactive'}},{$project:{name:1,language:1}}]).pretty();
db.users.aggregate([{$unwind:'$language'},{$group:{_td:'$language'}}]);

* lookup
- Joining two tables
 
db.users.aggregate({
    $lookup:{
        from: "department",
        localField:"dept", 06
        foreignField: "name"
        as: "anything"
})

* out 
- It will create the new collection with that name and storing the data result into it. 
db.users.aggregate([{ $match:{status:'active'}},{$project:{email:1,name:1, id:0}},{'$out':'info'}]).pretty();
Comment

mongodb aggregate project

db.books.aggregate( [ { $project : { title : 1 , author : 1 } } ] )
Comment

mongodb aggregate

let videos= await Beg.aggregate([
        {
          $project: {
            _id: 1,
          },
        },
        {
          $lookup: {
            from: "videos",
            localField: "_id",
            foreignField: "begId",
            as: "videos",
          },
        },
      ]);
Comment

PREVIOUS NEXT
Code Example
Javascript :: react currency format method 
Javascript :: javascript print to pdf 
Javascript :: how to check the number is palindrome or not 
Javascript :: pass data from child component to parent component 
Javascript :: firebase sign up with email and password 
Javascript :: try catch with for loop in javascript 
Javascript :: js max array 
Javascript :: how to push two values in array at once 
Javascript :: longitud objeto javascript 
Javascript :: get max value of slider js 
Javascript :: random color js 
Javascript :: select id get option value jquery 
Javascript :: javascript for loop[ 
Javascript :: run javascript sublime text 3 
Javascript :: nestjs custom error class validator 
Javascript :: Detect the city on application launch via geolocation react native 
Javascript :: sanitize data within an Express application 
Javascript :: js difference between two arrays of objects 
Javascript :: javascript remove required attribute 
Javascript :: how to attach function to button sweetalert2 
Javascript :: how to make a alert popup message in javascript 
Javascript :: how to check if json data is received in ajax response 
Javascript :: js string to blob 
Javascript :: button disappears after click javascript 
Javascript :: mail 
Javascript :: check if the difference between two dates is more than 1 month in javascript 
Javascript :: array.push 
Javascript :: js object.entries with sort 
Javascript :: vuejs take rgba values from coordinate 
Javascript :: how make calender in bootstrap 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =