Reputation: 993
I made a question related with this, but I have a new doubt with the Aggregation Framework with mongoose. For example, I have this schema:
var userschema = new mongoose.Schema({
user: String,
pass: String,
following: [String],
imagen: [{
title: String,
date: { type: Date, default: Date.now },
name: String,
author: String,
}],
});
And this is the code with the Aggregation Framework:
app.get('/home', middleware.yeses, function (req, res){
usermodel.findOne({ user: req.session.user }, function (err, user){
model.aggregate([
{$match: { _id : '50f5c7265838530000000006' }},
{$unwind: '$imagen'},
{$sort: {'imagen.date': 1}}
], function (err, imagenes){
if (err) throw err;
console.log(imagenes);
res.send('foo');
});
});
});
I'm trying to get in the console all the images
, just the imagen schema like this for example:
{ title: 'footitle',
name: 'foophoto.png',
date: Tue Jan 1 2013 22:32:12 GMT+0100 (CET),
author: 'foouser' }
And get all the imagen schemas, ordered by date.
The following array at the user's schema, contains the users' _id
s that the actual user is following. So, with {$match: { _id : '50f5c7265838530000000006' }},
, I get only the images of the users I'm following. The problem is that, when this code is execute, I receive this []
, the array of images that I get is empty, so the code doesn't work, because the users that I'm following have upload photos.
When I delete this {$match: { _id : '50f5c7265838530000000006' }}
, I get all the users, and their imagens, but I don't get only the imagen Schema, I get the whole user schema.
So, what I want, is to get all the images, of the users that I'm following, ordered by date. Is there any solution for this?
Thank's advance!
Upvotes: 2
Views: 2246
Reputation: 17960
The reason for this is that you are passing the _id
field as a string instead of an ObjectID
. Try this:
var ObjectID = require("mongodb").ObjectID;
// ...
model.aggregate([
{$match: { _id : ObjectID('50f5c7265838530000000006') }},
// ...
Upvotes: 4