我有一个架构是:
var photoSchema = new mongoose.Schema({
id: String, // Unique ID identifying this photo
file_name: String,
date_time: {type: Date, default: Date.now},
user_id: mongoose.Schema.Types.ObjectId, // The user id of the user who created the photo.
comments: [commentSchema] // Comment objects representing the comments made on this photo.
});
var Photo = mongoose.model('Photo', photoSchema);
Run Code Online (Sandbox Code Playgroud)
我正在尝试查找评论数量最多的特定用户的照片。这是我所做的:
Photo.aggregate([
{
$project: {
id: 1,
file_name: 1,
date_time: 1,
user_id: 1,
comments: 1,
length: {$size: "$comments"}
}
},
{
$match: {
user_id: mongoose.Schema.Types.ObjectId(request.params.id)
}
},
{ …Run Code Online (Sandbox Code Playgroud)