2016-06-13 66 views
1

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

你能不能從蒙戈張貼滿後的文件? – profesor79

+0

你的意思是發佈文章的整個模式? – unwosu

+0

db.post.findOne()是確定的 - 請刪除敏感信息 – profesor79

回答

2

要返回由ID列表描述用戶創建的所有帖子,使用$in運營商在查詢中,然後鏈sort()方法查詢命令的結果通過創建日期字段:

Post.find({ "author": { "$in": userIds } }) 
    .sort("-created") // or .sort({ field: 'asc', created: -1 }); 
    .exec(function (err, data){ 
     if(error){ 
      return console.log(error); 
     } else { 
      return console.log(data); 
     } 
    }); 

爲了讓您擁有的帖子ID的每用戶分組的結果,你需要運行在聚集操作:

Post.aggregate([ 
    { "$match:" { "author": { "$in": userIds } } }, 
    { "$sort": { "created": -1 } }, 
    { 
     "$group": { 
      "_id": "$author", 
      "posts": { "$push": "$_id" } 
     } 
    }, 
    { 
     "$project": { 
      "_id": 0, 
      "userId": "$_id", 
      "posts": 1 
     } 
    } 
]).exec(function (err, result){ 
    if(error){ 
     return console.log(error); 
    } else { 
     return console.log(result); 
    } 
}); 

或者用流利的API:

Post.aggregate() 
    .match({ "author": { "$in": userIds } }) 
    .sort("-created") 
    .group({ 
     "_id": "$author", 
     "posts": { "$push": "$_id" } 
    }) 
    .project({ 
     "_id": 0, 
     "userId": "$_id", 
     "posts": 1 
    }) 
    .exec(function (err, result){ 
     if(error){ 
      return console.log(error); 
     } else { 
      return console.log(result); 
     } 
    }); 
+0

有沒有一種方法可以根據用戶提供的ids將該帖子分組?基本上與單個用戶的帖子匹配像這樣。{userAId:「56656」,post:[{postA,postB}],userBId:「xys」,post:[postA,postB]} – unwosu

+0

從數據庫中獲取帖子會更好嗎?代碼中的連接? – unwosu

+0

@ user34 12942你可以請更新你的問題,因爲原來的問題_我想返回這些用戶創建的所有帖子。帖子應按其創建日期排序。我如何編寫這個查詢?_解決了,不是嗎?請記住,如果在已經有答案的情況下改變你的問題,這被認爲是不好的做法 - [** chameleon questions **](http://meta.stackexchange.com/questions/43478/exit-strategies-for-變色龍的問題/)。 – chridam

0

這應該是可能沒有聚合。

Post 
.find({ author: { $in: userIds } }) 
.sort({ created: -1 }) 

如果你得到CastError:投射到的ObjectId失敗,請確保您的用戶id從字符串數組數組映射到貓鼬ID的數組。

用戶id = userIds.map(用戶ID =>新mongoose.Types.ObjectId(用戶ID))

+0

有沒有辦法根據用戶ID提供的這個帖子?基本上匹配這樣的個人用戶的帖子。 {userAId:「56656」,post:[{postA,postB}],userBId:「xys」,post:[postA,postB]} – unwosu