Guest User

Untitled

a guest
Aug 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. var Wizard = new Schema({
  2. name : { type: String }
  3. , spells : { [{ type: Schema.ObjectId, ref: 'Spell' }] }
  4. });
  5.  
  6. var Spell = new Schema({
  7. name : { type: String }
  8. , damages : { type: Number }
  9. });
  10.  
  11. [{
  12. name: 'Gandalf',
  13. spells: [{
  14. name: 'Fireball',
  15. damages: 20
  16. }]
  17. }, {
  18. name: 'Saruman',
  19. spells: [{
  20. name: 'Frozenball',
  21. damages: 10
  22. }]
  23. }, {
  24. name: 'Radagast',
  25. spells: [{
  26. name: 'Lightball',
  27. damages: 15
  28. }]
  29. }]
  30.  
  31. WizardModel
  32. .find({})
  33. .populate('spells', myfields, myconditions, { sort: [['damages', 'asc']] })
  34. // Should return in the right order: Saruman, Radagast, Gandalf
  35.  
  36. WizardModel
  37. .find({})
  38. .populate({path: 'spells', options: { sort: [['damages', 'asc']] }})
  39.  
  40. doc
  41. .populate('company')
  42. .populate({
  43. path: 'notes',
  44. match: /airline/,
  45. select: 'text',
  46. model: 'modelName'
  47. options: opts
  48. }, function (err, user) {
  49. assert(doc._id == user._id) // the document itself is passed
  50. })
  51.  
  52. {
  53. $lookup: {
  54. from: 'spells',
  55. localField: 'spells',
  56. foreignField:'_id',
  57. as: 'spells'
  58. }
  59. },
  60. {
  61. $project: {
  62. _id: 1,
  63. name: 1,
  64. // project the values from damages in the spells array in a new array called damages
  65. damages: '$spells.damages',
  66. spells: {
  67. name: 1,
  68. damages: 1
  69. }
  70. }
  71. },
  72. // take the maximum damage from the damages array
  73. {
  74. $project: {
  75. _id: 1,
  76. spells: 1,
  77. name: 1,
  78. maxDamage: {$max: '$damages'}
  79. }
  80. },
  81. // do the sorting
  82. {
  83. $sort: {'maxDamage' : -1}
  84. }
  85.  
  86. 'use strict';
  87.  
  88. const mongoose = require('mongoose');
  89. const Schema = mongoose.Schema;
  90.  
  91. mongoose.connect('mongodb://localhost/lotr');
  92.  
  93. const db = mongoose.connection;
  94.  
  95. db.on('error', console.error.bind(console, 'connection error:'));
  96. db.once('open', () => {
  97.  
  98.  
  99.  
  100. let SpellSchema = new Schema({
  101. name : { type: String },
  102. damages : { type: Number }
  103. });
  104.  
  105. let Spell = mongoose.model('Spell', SpellSchema);
  106.  
  107. let WizardSchema = new Schema({
  108. name: { type: String },
  109. spells: [{ type: Schema.Types.ObjectId, ref: 'Spell' }]
  110. });
  111.  
  112. let Wizard = mongoose.model('Wizard', WizardSchema);
  113.  
  114. let fireball = new Spell({
  115. name: 'Fireball',
  116. damages: 20
  117. });
  118.  
  119. let frozenball = new Spell({
  120. name: 'Frozenball',
  121. damages: 10
  122. });
  123.  
  124. let lightball = new Spell({
  125. name: 'Lightball',
  126. damages: 15
  127. });
  128.  
  129. let spells = [fireball, frozenball, lightball];
  130.  
  131. let wizards = [{
  132. name: 'Gandalf',
  133. spells:[fireball]
  134. }, {
  135.  
  136. name: 'Saruman',
  137. spells:[frozenball]
  138. }, {
  139. name: 'Radagast',
  140. spells:[lightball]
  141. }];
  142.  
  143. let aggregation = [
  144. {
  145. $match: {}
  146. },
  147. // find all spells in the spells collection related to wizards and fill populate into wizards.spells
  148. {
  149. $lookup: {
  150. from: 'spells',
  151. localField: 'spells',
  152. foreignField:'_id',
  153. as: 'spells'
  154. }
  155. },
  156. {
  157. $project: {
  158. _id: 1,
  159. name: 1,
  160. // project the values from damages in the spells array in a new array called damages
  161. damages: '$spells.damages',
  162. spells: {
  163. name: 1,
  164. damages: 1
  165. }
  166. }
  167. },
  168. // take the maximum damage from the damages array
  169. {
  170. $project: {
  171. _id: 1,
  172. spells: 1,
  173. name: 1,
  174. maxDamage: {$max: '$damages'}
  175. }
  176. },
  177. // do the sorting
  178. {
  179. $sort: {'maxDamage' : -1}
  180. }
  181. ];
  182. Spell.create(spells, (err, spells) => {
  183. if (err) throw(err);
  184. else {
  185. Wizard.create(wizards, (err, wizards) =>{
  186. if (err) throw(err);
  187. else {
  188. Wizard.aggregate(aggregation)
  189. .exec((err, models) => {
  190. if (err) throw(err);
  191. else {
  192. console.log(models[0]); // eslint-disable-line
  193. console.log(models[1]); // eslint-disable-line
  194. console.log(models[2]); // eslint-disable-line
  195. Wizard.remove().exec(() => {
  196. Spell.remove().exec(() => {
  197. process.exit(0);
  198. });
  199. });
  200. }
  201. });
  202. }
  203. });
  204. }
  205. });
  206. });
  207.  
  208. var PersonSchema = new Schema({
  209. name: String,
  210. band: String
  211. });
  212.  
  213. var BandSchema = new Schema({
  214. name: String
  215. });
  216. BandSchema.virtual('members', {
  217. ref: 'Person', // The model to use
  218. localField: 'name', // Find people where `localField`
  219. foreignField: 'band', // is equal to `foreignField`
  220. // If `justOne` is true, 'members' will be a single doc as opposed to
  221. // an array. `justOne` is false by default.
  222. justOne: false,
  223. options: { sort: { name: -1 }, limit: 5 }
  224. });
Add Comment
Please, Sign In to add comment