Created
December 18, 2011 19:47
-
-
Save FrancescaK/1494268 to your computer and use it in GitHub Desktop.
Pipeline operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple matching | |
var m1 = db.runCommand( | |
{ aggregate : "article", pipeline : [ | |
{ $match : { author : "dave" } } | |
]}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" } } | |
]}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple projection | |
var p1 = db.runCommand( | |
{ aggregate : "article", pipeline : [ | |
{ $project : { | |
tags : 1, | |
pageViews : 1 | |
}} | |
]}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple matching | |
var m1 = db.runCommand( | |
{ aggregate : "article", pipeline : [ | |
{ $match : { author : "dave" } } | |
]}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" } } | |
]}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// simple projection | |
var p1 = db.runCommand( | |
{ aggregate : "article", pipeline : [ | |
{ $project : { | |
tags : 1, | |
pageViews : 1 | |
}} | |
]}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] , | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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