deno.land / x / mongoose@6.7.5 / examples / population / population-basic.js

population-basic.js
نووسراو ببینە
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

'use strict';const mongoose = require('../../lib');const Schema = mongoose.Schema;
console.log('Running mongoose version %s', mongoose.version);
/** * Console schema */
const consoleSchema = Schema({ name: String, manufacturer: String, released: Date});const Console = mongoose.model('Console', consoleSchema);
/** * Game schema */
const gameSchema = Schema({ name: String, developer: String, released: Date, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]});const Game = mongoose.model('Game', gameSchema);
/** * Connect to the console database on localhost with * the default port (27017) */
mongoose.connect('mongodb://localhost/console', function(err) { // if we failed to connect, abort if (err) throw err;
// we connected ok createData();});
/** * Data generation */
function createData() { Console.create( { name: 'Nintendo 64', manufacturer: 'Nintendo', released: 'September 29, 1996' }, function(err, nintendo64) { if (err) return done(err);
Game.create({ name: 'Legend of Zelda: Ocarina of Time', developer: 'Nintendo', released: new Date('November 21, 1998'), consoles: [nintendo64] }, function(err) { if (err) return done(err); example(); }); } );}
/** * Population */
function example() { Game .findOne({ name: /^Legend of Zelda/ }) .populate('consoles') .exec(function(err, ocinara) { if (err) return done(err);
console.log( '"%s" was released for the %s on %s', ocinara.name, ocinara.consoles[0].name, ocinara.released.toLocaleDateString() );
done(); });}
function done(err) { if (err) console.error(err); Console.remove(function() { Game.remove(function() { mongoose.disconnect(); }); });}
mongoose

Version Info

Tagged at
a year ago