Blame view

node_modules/mongoose/examples/population-across-three-collections.js 2.58 KB
46de8790   Tarpit Grover   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

var assert = require('assert')
var mongoose = require('../');
var Schema = mongoose.Schema;
var ObjectId = mongoose.Types.ObjectId;

/**
 * Connect to the db
 */

var dbname = 'testing_populateAdInfinitum_' + require('../lib/utils').random()
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function() {
  console.error('connection error', arguments);
});

/**
 * Schemas
 */

var user = new Schema({
  name: String,
  friends: [{
    type: Schema.ObjectId,
    ref: 'User'
  }]
});
var User = mongoose.model('User', user);

var blogpost = Schema({
  title: String,
  tags: [String],
  author: {
    type: Schema.ObjectId,
    ref: 'User'
  }
})
var BlogPost = mongoose.model('BlogPost', blogpost);

/**
 * example
 */

mongoose.connection.on('open', function() {

  /**
   * Generate data
   */

  var userIds = [new ObjectId, new ObjectId, new ObjectId, new ObjectId];
  var users = [];

  users.push({
    _id: userIds[0],
    name: 'mary',
    friends: [userIds[1], userIds[2], userIds[3]]
  });
  users.push({
    _id: userIds[1],
    name: 'bob',
    friends: [userIds[0], userIds[2], userIds[3]]
  });
  users.push({
    _id: userIds[2],
    name: 'joe',
    friends: [userIds[0], userIds[1], userIds[3]]
  });
  users.push({
    _id: userIds[3],
    name: 'sally',
    friends: [userIds[0], userIds[1], userIds[2]]
  });

  User.create(users, function(err, docs) {
    assert.ifError(err);

    var blogposts = [];
    blogposts.push({
      title: 'blog 1',
      tags: ['fun', 'cool'],
      author: userIds[3]
    })
    blogposts.push({
      title: 'blog 2',
      tags: ['cool'],
      author: userIds[1]
    })
    blogposts.push({
      title: 'blog 3',
      tags: ['fun', 'odd'],
      author: userIds[2]
    })

    BlogPost.create(blogposts, function(err, docs) {
      assert.ifError(err);

      /**
       * Population
       */

      BlogPost
      .find({ tags: 'fun' })
      .lean()
      .populate('author')
      .exec(function(err, docs) {
        assert.ifError(err);

        /**
         * Populate the populated documents
         */

        var opts = {
          path: 'author.friends',
          select: 'name',
          options: { limit: 2 }
        }

        BlogPost.populate(docs, opts, function(err, docs) {
          assert.ifError(err);
          console.log('populated');
          var s = require('util').inspect(docs, { depth: null })
          console.log(s);
          done();
        })
      })
    })
  })
});

function done(err) {
  if (err) console.error(err.stack);
  mongoose.connection.db.dropDatabase(function() {
    mongoose.connection.close();
  });
}