的阵列我有看起来像这样猫鼬聚合匹配的ObjectID
var Post = new mongoose.Schema({
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
created: {
type: Date,
Default: Date.now
})
我有一个用户表,以及一个模式。我有用户ID的数组,我试图基于搜索的用户的阵列上的职位表ID
对于实例
var userIds = ["575e96652473d2ab0ac51c1e","575e96652473d2ab0ac51c1d"] .... and so on
我要回这些用户创建的所有帖子。帖子应按其创建日期排序。有没有办法根据提供的用户标识对这篇文章进行分组,基本上与单个用户的帖子相匹配?
我想达到的结果是这样的:
[{
userAId : "56656.....",
post : [postA, postB],
},{
userBId :"12345...",
post : [postA, postB]
}]
我怎样写这个查询?
这是我迄今为止
Post.aggregate([{
// {"$unwind" : ""},
// "$group": {
// _id: "$author",
// "created" : {"$sum" : 1 }
// }
"$match" : { author : id}
}]).exec(function(error, data) {
if(error){
return console.log(error);
}else{
return console.log(data)
}
})
{
"_id" : ObjectId("575e95bc2473d2ab0ac51c1b"),
"lastMod" : ISODate("2016-06-13T11:15:08.950Z"),
"author" : ObjectId("575dac62ec13010678fe41cd"),
"created" : ISODate("2016-06-13T11:15:08.947Z"),
"type" : "photo",
"end" : null,
"commentCount" : 0,
"viewCount" : 0,
"likes" : 0,
"tags" : [],
"title" : "Today is a good day",
"__v" : 0
}
你能不能从蒙戈张贴满后的文件? – profesor79
你的意思是发布文章的整个模式? – unwosu
db.post.findOne()是确定的 - 请删除敏感信息 – profesor79