{"id":6165,"date":"2014-04-13T04:36:15","date_gmt":"2014-04-13T04:36:15","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/cant-use-match-with-mongoose-and-the-aggregation-framework-collection-of-common-programming-errors-2\/"},"modified":"2014-04-13T04:36:15","modified_gmt":"2014-04-13T04:36:15","slug":"cant-use-match-with-mongoose-and-the-aggregation-framework-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/cant-use-match-with-mongoose-and-the-aggregation-framework-collection-of-common-programming-errors-2\/","title":{"rendered":"Can&#39;t use $match with mongoose and the aggregation framework-Collection of common programming errors"},"content":{"rendered":"<p>This is my Schema:<\/p>\n<pre><code>var userschema = new mongoose.Schema({\n\n  user: String,\n  follow: [String],\n  imagen: [{ \n\n              title: String,\n              date: { type: Date, default: Date.now }\n\n           }]\n });\n<\/code><\/pre>\n<p>And this is the code:<\/p>\n<pre><code> usermodel.findOne({ user: req.session.user }, function (err, user){\n  usermodel.aggregate({$unwind: '$imagen'}, \n                   {$match: { _id: { $in: user.follow } }}, \n                   {imagen: true}, \n                   {$sort: {'imagen.date': 1}}, \n                    function (err, images){\n\n                     console.log(images);              \n\n                      res.render('home.ejs', {\n\n                       user: user,\n                       following: images\n\n                      });\n   });\n  });\n<\/code><\/pre>\n<p>The <code>follow<\/code> contains users&#8217;s <code>_id<\/code>.<\/p>\n<p>The code works, except when I include the <code>$match<\/code>. I use the <code>$match<\/code>to filter the result, only getting the images of the user that I&#8217;m following, but the console.log show me that the result of the <code>aggregate<\/code> search is undefined, but when I don&#8217;t write the <code>$match<\/code> query, I get the images, but I obtain all the images, not only the images of the user that I&#8217;m following.<\/p>\n<p>Is there any solution for this&#8230;?<\/p>\n<p>Thank&#8217;s advance!<\/p>\n<p>EDIT:<\/p>\n<pre><code>var express = require('express');\nvar MongoStore = require('connect-mongo')(express);\nvar fs = require('fs');\nvar mongoose = require('mongoose');\n\nvar app = express();\napp.listen(9191);\n\nvar sessionStore = new MongoStore({db: 'session'});\n\napp.configure(function(){\n\n   app.use(express.bodyParser());\n   app.set('views',__dirname + '\/views');\n   app.set('view engine', 'ejs');\n   app.use(express.static(__dirname + '\/public'));\n   app.use(express.cookieParser());\n   app.use(express.session({\n     store: sessionStore,\n     secret: 'secret'\n   }));\n   app.use(app.router);\n\n});\n\nvar db = mongoose.createConnection('localhost', 'test');\n\nvar userschema = new mongoose.Schema({\n\n  user: String,\n  follow: [String],\n  imagen: [{ \n\n              title: String,\n              date: { type: Date, default: Date.now }\n\n           }]\n });\n\nvar usermodel =  db.model('usermodel', userschema);\nvar ObjectId = require('mongoose').Types.ObjectId;\n\napp.get('\/', middleware.noses, function (req, res){\n\n     res.render('home0.ejs');\n\n});\n\n\napp.get('\/home', middleware.yeses, function (req, res){\n\n  usermodel.findOne({ user: req.session.user }, function (err, user){\n\n    if (user.follow.length != 0){\n\n      usermodel.find({ _id: {$in: user.follow } }, { user: true }, function (err, users){\n\n         var usernames = users.map(function(u){ return u.user });\n\n          usermodel.aggregate({$match: { _id: { $in: user.follow.map(\n                                           function(id){ return new ObjectId(id); })}}},\n                                       {$unwind: '$imagen'}, \n                                       {imagen: true}, \n                                       {$sort: {'imagen.date': 1}}, \n                                        function (err, images){\n\n                                           console.log(images);\n\n                                          res.render('home.ejs', {\n\n                                             user: user,\n                                             following: images\n\n                                        });\n               });\n            });\n\n    }  else {\n\n       res.render('home.ejs', {\n\n              user: user,\n              following: undefined\n\n            });\n\n     }\n\n });\n});\n<\/code><\/pre>\n<p>EDIT:<\/p>\n<pre><code>[ { __v: 4,\n   _id: 50fd9c7b8e6a9d087d000006,\n   follow: ['50fd9cbd1322de627d000006', '50fd9d3ce20da1dd7d000006'],\n   imagen: \n   [{ title: 'foo',\n      _id: 50fd9ca2bc9f163e7d000006,\n      date: Mon Jan 21 2013 20:53:06 GMT+0100 (CET) },\n    { title: 'foot',\n      _id: 50fda83a3214babc88000005,\n      date: Mon Jan 21 2013 21:42:34 GMT+0100 (CET) }],\n   user: 'Mrmangado' }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is my Schema: var userschema = new mongoose.Schema({ user: String, follow: [String], imagen: [{ title: String, date: { type: Date, default: Date.now } }] }); And this is the code: usermodel.findOne({ user: req.session.user }, function (err, user){ usermodel.aggregate({$unwind: &#8216;$imagen&#8217;}, {$match: { _id: { $in: user.follow } }}, {imagen: true}, {$sort: {&#8216;imagen.date&#8217;: 1}}, function (err, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6165","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6165"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6165\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}