Guest User

Untitled

a guest
Dec 14th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. [
  2. { "_id":1, "make":"Honda", "model":"Accord" },
  3. { "_id":2, "make":"Toyota", "model":"Camry" },
  4. { "_id":3, "make":"Honda", "model":"Civic" },
  5. { "_id":4, "make":"Subaru", "model":"Legacy" },
  6. { "_id":5, "make":"Subaru", "model":"Outback" },
  7. { "_id":6, "make":"Toyota", "model":"Corrola" }
  8. ]
  9.  
  10. [
  11. { "description" : "Honda" },
  12. { "description" : "Honda - Accord" },
  13. { "description" : "Honda - Civic" },
  14. { "description" : "Subaru" },
  15. { "description" : "Subaru - Legacy" },
  16. { "description" : "Subaru - Outback" },
  17. { "description" : "Toyota" },
  18. { "description" : "Toyota - Camry" },
  19. { "description" : "Toyota - Corrola" }
  20. ]
  21.  
  22. db.cars.aggregate([
  23. { $sort : { make : 1, model:1 } },
  24. { $project: {"_id":0, "description":{ $concat: [ "$make", " - ", "$model" ]} } }
  25. ])
  26.  
  27. [
  28. { "description" : "Honda - Accord" },
  29. { "description" : "Honda - Civic" },
  30. { "description" : "Subaru - Legacy" },
  31. { "description" : "Subaru - Outback" },
  32. { "description" : "Toyota - Camry" },
  33. { "description" : "Toyota - Corrola" }
  34. ]
  35.  
  36. db.cars.aggregate([
  37. { $sort: { make: 1, model: 1 }},
  38. { $group: {
  39. "_id": "$make",
  40. models: {
  41. $push: {
  42. $concat: ["$make", " - ", "$model"]
  43. }
  44. }
  45. }
  46. },
  47. { $project: {
  48. "_id": 0,
  49. "description": {
  50. $reduce: {
  51. input: "$models",
  52. initialValue: "$_id",
  53. in: {
  54. $concatArrays: ["$$value", "$$this"]
  55. }
  56. }
  57. }
  58. }
  59. }
  60. ])
  61.  
  62. "errmsg" : "$concatArrays only supports arrays, not string",
  63. "code" : 28664,
Add Comment
Please, Sign In to add comment