Guest User

Untitled

a guest
Jan 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. userId i am getting from the route
  2.  
  3.  
  4.  
  5. Message.aggregate(
  6. [
  7. { $match: { $or: [{ 'from.userId': userId }, { 'to.userId': userId }] } },
  8. // { $project: {...FIELDS}}, ///what to use here
  9. { $sort: { timestamp: -1 } },
  10. {
  11. $group: {
  12. "_id": {
  13. 'fromuserId': userId,//confusion here
  14. // 'toUserId': userId,
  15. // 'to.user'
  16. },
  17. // 'userId': { $first: '$from' },
  18. // 'userId': { $first: '$to' },
  19. msg: { $first: '$msg' },
  20. timestamp: { $first: '$timestamp' }
  21. }
  22. }
  23. ]
  24. ).exec((err, results) => {
  25. if (err) {
  26. console.log('err===>', err);
  27. }
  28. console.log('results|||', results)
  29. })
  30.  
  31. my schema is this :
  32.  
  33.  
  34.  
  35.  
  36.  
  37. const MessageSchema = new mongoose.Schema({
  38. to: {
  39. userId: {
  40. type: mongoose.Schema.Types.ObjectId,// to be sent from client side
  41. ref: "User",
  42. required: true
  43. },
  44. name: {
  45. type: String,
  46. },
  47. pictureUrl: {
  48. type: String,
  49. }
  50. },
  51. from: {
  52. userId: {
  53. type: mongoose.Schema.Types.ObjectId,// to be sent from server side
  54. ref: "User",
  55. required: true
  56. },
  57. name: {
  58. type: String,
  59. },
  60. pictureUrl: {
  61. type: String,
  62. }
  63. },
  64. message: {
  65. type: String,
  66. required: true
  67. }
  68. },
  69. {
  70. timestamps: true
  71. }
  72. );
Add Comment
Please, Sign In to add comment