2016-06-13 121 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