的陣列我有看起來像這樣貓鼬聚合匹配的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