Skip to content

Instantly share code, notes, and snippets.

@FrancescaK
Created December 18, 2011 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrancescaK/1494268 to your computer and use it in GitHub Desktop.
Save FrancescaK/1494268 to your computer and use it in GitHub Desktop.
Pipeline operations
// simple matching
var m1 = db.runCommand(
{ aggregate : "article", pipeline : [
{ $match : { author : "dave" } }
]});
// combining matching with a projection
var m2 = db.runCommand(
{ aggregate : "article", pipeline : [
{ $project : {
title : 1,
author : 1,
pageViews : 1,
tags : 1,
comments : 1
}},
{ $unwind : "$tags" },
{ $match : { tags : "nasty" } }
]});
// simple projection
var p1 = db.runCommand(
{ aggregate : "article", pipeline : [
{ $project : {
tags : 1,
pageViews : 1
}}
]});
db.article.save( {
title : "this is your title" ,
author : "dave" ,
posted : new Date(4121381470000) ,
pageViews : 7 ,
tags : [ "fun" , "nasty" ] ,
comments : [
{ author :"barbara" , text : "this is interesting" } ,
{ author :"jenny" , text : "i like to play pinball", votes: 10 }
],
other : { bar : 14 }
});
db.article.save( {
title : "this is some other title" ,
author : "jane" ,
posted : new Date(978239834000) ,
pageViews : 6 ,
tags : [ "nasty" , "filthy" ] ,
comments : [
{ author :"will" , text : "i don't like the color" } ,
{ author :"jenny" , text : "can i get that in green?" }
],
other : { bar : 14 }
});
// simple matching
var m1 = db.runCommand(
{ aggregate : "article", pipeline : [
{ $match : { author : "dave" } }
]});
// combining matching with a projection
var m2 = db.runCommand(
{ aggregate : "article", pipeline : [
{ $project : {
title : 1,
author : 1,
pageViews : 1,
tags : 1,
comments : 1
}},
{ $unwind : "$tags" },
{ $match : { tags : "nasty" } }
]});
// simple projection
var p1 = db.runCommand(
{ aggregate : "article", pipeline : [
{ $project : {
tags : 1,
pageViews : 1
}}
]});
title : "this is your title" ,
author : "dave" ,
posted : new Date(4121381470000) ,
pageViews : 7 ,
tags : [ "fun" ] ,
});
title : "this is your title" ,
author : "dave" ,
posted : new Date(4121381470000) ,
pageViews : 7 ,
tags : [ "nasty" ] ,
});
title : "this is some other title" ,
author : "jane" ,
posted : new Date(978239834000) ,
pageViews : 6 ,
tags : [ "nasty"] ,
});
title : "this is some other title" ,
author : "jane" ,
posted : new Date(978239834000) ,
pageViews : 6 ,
tags : [ "filthy"] ,
});
// unwinding an array
var u1 = db.runCommand(
{ aggregate : "article", pipeline : [
{ $unwind : "$tags" }
]});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment