rushabhmadhu

mongoose_query

Aug 27th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. We are struct at one mongodb point which i would like to describe here,
  2.  
  3. We are using mongoose 5.4 and create a model like below :
  4.  
  5.  
  6. var userSchema = mongoose.Schema({
  7. id:{ type: Number, default: 1 },
  8. first_name: String,
  9. last_name: String,
  10. mail: String,
  11. password: String,
  12. dob: { type: String, default: '' },
  13. gender: { type: String, default: '' },
  14. profile_photo: { type: String, default: '' },
  15. ethnicity: { type: String, default: '' },
  16. contact_number: { type: String, default: '' },
  17. user_type: Number,
  18. address1: { type: String, default: '' },
  19. address2: { type: String, default: '' },
  20. area: { type: String, default: '' },
  21. city: { type: String, default: '' },
  22. country: { type: String, default: '' },
  23. postcode: { type: String, default: '' },
  24. business_name: { type: String, default: '' },
  25. ip_address: String,
  26. status: Number,
  27. tag_line: { type: String, default: '' },
  28. is_influencer: Number,
  29. wallet_id: String,
  30. token_balance: { type: Number, default: 0 },
  31. point_balance: { type: Number, default: 0 },
  32. badges: [String],
  33. membership: { type: Schema.Types.ObjectId, ref: 'Membership' },
  34. transaction: [{ type: Schema.Types.ObjectId, ref: 'Transaction' }],
  35. property: [{ type: Schema.Types.ObjectId, ref: 'Property' }],
  36. reviews: [{ type: Schema.Types.ObjectId, ref: 'Review' }],
  37. created_date: String,
  38. });
  39. var User = mongoose.model('User', userSchema);
  40.  
  41. //Property Schema (Location schema)
  42. var propertySchema = mongoose.Schema({
  43. id: Number,
  44. property_name: String,
  45. address1: String,
  46. area: String,
  47. post_code: String,
  48. category: [{ type: Schema.Types.ObjectId, ref: 'Category' }],
  49. category_id: [Number],
  50. property_desc: String,
  51. property_images: String,
  52. slug : String,
  53. user_id: Number,
  54. business_key: String,
  55. user: { type: Schema.Types.ObjectId, ref: 'User' },
  56. reviews: [{ type: Schema.Types.ObjectId, ref: 'Review' }],
  57. status: Number,
  58. is_claimed: Number,
  59. created_date: String,
  60. });
  61. var Property = mongoose.model('Property', propertySchema);
  62.  
  63. var categorySchema = mongoose.Schema({
  64. id: Number,
  65. category_name: String,
  66. status: Number,
  67. user: { type: Schema.Types.ObjectId, ref: 'User' },
  68. property: [{ type: Schema.Types.ObjectId, ref: 'Property' }],
  69. created_date: String,
  70. updated_date: String
  71. });
  72. var Category = mongoose.model('Category', categorySchema);
  73.  
  74. we are also using async-await for this project. we are fetching locations using below method to get location data as well User and Category.
  75.  
  76. sortClause = {};
  77. sortClause.user=parseInt(-1);
  78. let properties = await Property.find({'status':{$in:[1,2]}}).populate({path: 'user',
  79. model: 'User',select: 'first_name last_name mail contact_number'}).populate({path: 'category',
  80. model: 'Category',select: 'category_name id'}).sort(sortClause).skip(skip).limit(perpage).exec();
  81.  
  82.  
  83. so when we get data in properties object after the execution of above line we are getting properties but as we are sorting by user's column of property model it does fetch data properly. It sorts on objectID and we would like to fetch on user's first_name.
  84. How we can get properies based on user's first_name column? Is there any suggestion for this problem.
Add Comment
Please, Sign In to add comment