population-across-three-collections.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. var assert = require('assert');
  2. var mongoose = require('../../lib');
  3. var Schema = mongoose.Schema;
  4. var ObjectId = mongoose.Types.ObjectId;
  5. /**
  6. * Connect to the db
  7. */
  8. var dbname = 'testing_populateAdInfinitum_' + require('../../lib/utils').random();
  9. mongoose.connect('localhost', dbname);
  10. mongoose.connection.on('error', function() {
  11. console.error('connection error', arguments);
  12. });
  13. /**
  14. * Schemas
  15. */
  16. var user = new Schema({
  17. name: String,
  18. friends: [{
  19. type: Schema.ObjectId,
  20. ref: 'User'
  21. }]
  22. });
  23. var User = mongoose.model('User', user);
  24. var blogpost = Schema({
  25. title: String,
  26. tags: [String],
  27. author: {
  28. type: Schema.ObjectId,
  29. ref: 'User'
  30. }
  31. });
  32. var BlogPost = mongoose.model('BlogPost', blogpost);
  33. /**
  34. * example
  35. */
  36. mongoose.connection.on('open', function() {
  37. /**
  38. * Generate data
  39. */
  40. var userIds = [new ObjectId, new ObjectId, new ObjectId, new ObjectId];
  41. var users = [];
  42. users.push({
  43. _id: userIds[0],
  44. name: 'mary',
  45. friends: [userIds[1], userIds[2], userIds[3]]
  46. });
  47. users.push({
  48. _id: userIds[1],
  49. name: 'bob',
  50. friends: [userIds[0], userIds[2], userIds[3]]
  51. });
  52. users.push({
  53. _id: userIds[2],
  54. name: 'joe',
  55. friends: [userIds[0], userIds[1], userIds[3]]
  56. });
  57. users.push({
  58. _id: userIds[3],
  59. name: 'sally',
  60. friends: [userIds[0], userIds[1], userIds[2]]
  61. });
  62. User.create(users, function(err) {
  63. assert.ifError(err);
  64. var blogposts = [];
  65. blogposts.push({
  66. title: 'blog 1',
  67. tags: ['fun', 'cool'],
  68. author: userIds[3]
  69. });
  70. blogposts.push({
  71. title: 'blog 2',
  72. tags: ['cool'],
  73. author: userIds[1]
  74. });
  75. blogposts.push({
  76. title: 'blog 3',
  77. tags: ['fun', 'odd'],
  78. author: userIds[2]
  79. });
  80. BlogPost.create(blogposts, function(err) {
  81. assert.ifError(err);
  82. /**
  83. * Population
  84. */
  85. BlogPost
  86. .find({tags: 'fun'})
  87. .lean()
  88. .populate('author')
  89. .exec(function(err, docs) {
  90. assert.ifError(err);
  91. /**
  92. * Populate the populated documents
  93. */
  94. var opts = {
  95. path: 'author.friends',
  96. select: 'name',
  97. options: {limit: 2}
  98. };
  99. BlogPost.populate(docs, opts, function(err, docs) {
  100. assert.ifError(err);
  101. console.log('populated');
  102. var s = require('util').inspect(docs, {depth: null, colors: true});
  103. console.log(s);
  104. done();
  105. });
  106. });
  107. });
  108. });
  109. });
  110. function done(err) {
  111. if (err) console.error(err.stack);
  112. mongoose.connection.db.dropDatabase(function() {
  113. mongoose.connection.close();
  114. });
  115. }