mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-13 03:02:49 +00:00
Added all files
This commit is contained in:
41
node_modules/mongoose/examples/README.md
generated
vendored
Normal file
41
node_modules/mongoose/examples/README.md
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
This directory contains runnable sample mongoose programs.
|
||||
|
||||
To run:
|
||||
|
||||
- first install [Node.js](http://nodejs.org/)
|
||||
- from the root of the project, execute `npm install -d`
|
||||
- in the example directory, run `npm install -d`
|
||||
- from the command line, execute: `node example.js`, replacing "example.js" with the name of a program.
|
||||
|
||||
|
||||
Goal is to show:
|
||||
|
||||
- ~~global schemas~~
|
||||
- ~~GeoJSON schemas / use (with crs)~~
|
||||
- text search (once MongoDB removes the "Experimental/beta" label)
|
||||
- ~~lean queries~~
|
||||
- ~~statics~~
|
||||
- methods and statics on subdocs
|
||||
- custom types
|
||||
- ~~querybuilder~~
|
||||
- ~~promises~~
|
||||
- accessing driver collection, db
|
||||
- ~~connecting to replica sets~~
|
||||
- connecting to sharded clusters
|
||||
- enabling a fail fast mode
|
||||
- on the fly schemas
|
||||
- storing files
|
||||
- ~~map reduce~~
|
||||
- ~~aggregation~~
|
||||
- advanced hooks
|
||||
- using $elemMatch to return a subset of an array
|
||||
- query casting
|
||||
- upserts
|
||||
- pagination
|
||||
- express + mongoose session handling
|
||||
- ~~group by (use aggregation)~~
|
||||
- authentication
|
||||
- schema migration techniques
|
||||
- converting documents to plain objects (show transforms)
|
||||
- how to $unset
|
||||
|
||||
78
node_modules/mongoose/examples/aggregate/aggregate.js
generated
vendored
Normal file
78
node_modules/mongoose/examples/aggregate/aggregate.js
generated
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./person.js')();
|
||||
|
||||
var Person = mongoose.model('Person');
|
||||
|
||||
// define some dummy data
|
||||
var data = [
|
||||
{ name : 'bill', age : 25, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 25)), gender : "Male",
|
||||
likes : ['movies', 'games', 'dogs']},
|
||||
{ name : 'mary', age : 30, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 30)), gender : "Female",
|
||||
likes : ['movies', 'birds', 'cats']},
|
||||
{ name : 'bob', age : 21, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 21)), gender : "Male",
|
||||
likes : ['tv', 'games', 'rabbits']},
|
||||
{ name : 'lilly', age : 26, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 26)), gender : "Female",
|
||||
likes : ['books', 'cats', 'dogs']},
|
||||
{ name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 1000)), gender : "Male",
|
||||
likes : ['glasses', 'wine', 'the night']},
|
||||
];
|
||||
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// create all of the dummy people
|
||||
async.each(data, function (item, cb) {
|
||||
Person.create(item, cb);
|
||||
}, function (err) {
|
||||
|
||||
// run an aggregate query that will get all of the people who like a given
|
||||
// item. To see the full documentation on ways to use the aggregate
|
||||
// framework, see http://docs.mongodb.org/manual/core/aggregation/
|
||||
Person.aggregate(
|
||||
// select the fields we want to deal with
|
||||
{ $project : { name : 1, likes : 1 } },
|
||||
// unwind 'likes', which will create a document for each like
|
||||
{ $unwind : "$likes" },
|
||||
// group everything by the like and then add each name with that like to
|
||||
// the set for the like
|
||||
{ $group : {
|
||||
_id : { likes : "$likes" },
|
||||
likers : { $addToSet : "$name" }
|
||||
} },
|
||||
function (err, result) {
|
||||
if (err) throw err;
|
||||
console.log(result);
|
||||
//[ { _id: { likes: 'the night' }, likers: [ 'alucard' ] },
|
||||
//{ _id: { likes: 'wine' }, likers: [ 'alucard' ] },
|
||||
//{ _id: { likes: 'books' }, likers: [ 'lilly' ] },
|
||||
//{ _id: { likes: 'glasses' }, likers: [ 'alucard' ] },
|
||||
//{ _id: { likes: 'birds' }, likers: [ 'mary' ] },
|
||||
//{ _id: { likes: 'rabbits' }, likers: [ 'bob' ] },
|
||||
//{ _id: { likes: 'cats' }, likers: [ 'lilly', 'mary' ] },
|
||||
//{ _id: { likes: 'dogs' }, likers: [ 'lilly', 'bill' ] },
|
||||
//{ _id: { likes: 'tv' }, likers: [ 'bob' ] },
|
||||
//{ _id: { likes: 'games' }, likers: [ 'bob', 'bill' ] },
|
||||
//{ _id: { likes: 'movies' }, likers: [ 'mary', 'bill' ] } ]
|
||||
|
||||
cleanup();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
14
node_modules/mongoose/examples/aggregate/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/aggregate/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "aggregate-example",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "deps for aggregate example",
|
||||
"main": "aggregate.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "async": "*" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
17
node_modules/mongoose/examples/aggregate/person.js
generated
vendored
Normal file
17
node_modules/mongoose/examples/aggregate/person.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date,
|
||||
gender: String,
|
||||
likes: [String]
|
||||
});
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
70
node_modules/mongoose/examples/doc-methods.js
generated
vendored
Normal file
70
node_modules/mongoose/examples/doc-methods.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
var mongoose = require('mongoose')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Schema
|
||||
*/
|
||||
|
||||
var CharacterSchema = Schema({
|
||||
name: { type: String, required: true }
|
||||
, health: { type: Number, min: 0, max: 100 }
|
||||
})
|
||||
|
||||
/**
|
||||
* Methods
|
||||
*/
|
||||
|
||||
CharacterSchema.methods.attack = function () {
|
||||
console.log('%s is attacking', this.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Character model
|
||||
*/
|
||||
|
||||
var Character = mongoose.model('Character', CharacterSchema);
|
||||
|
||||
/**
|
||||
* Connect to the database on localhost with
|
||||
* the default port (27017)
|
||||
*/
|
||||
|
||||
var dbname = 'mongoose-example-doc-methods-' + ((Math.random()*10000)|0);
|
||||
var uri = 'mongodb://localhost/' + dbname;
|
||||
|
||||
console.log('connecting to %s', uri);
|
||||
|
||||
mongoose.connect(uri, function (err) {
|
||||
// if we failed to connect, abort
|
||||
if (err) throw err;
|
||||
|
||||
// we connected ok
|
||||
example();
|
||||
})
|
||||
|
||||
/**
|
||||
* Use case
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Character.create({ name: 'Link', health: 100 }, function (err, link) {
|
||||
if (err) return done(err);
|
||||
console.log('found', link);
|
||||
link.attack(); // 'Link is attacking'
|
||||
done();
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up
|
||||
*/
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
mongoose.connection.db.dropDatabase(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
}
|
||||
1
node_modules/mongoose/examples/express/README.md
generated
vendored
Normal file
1
node_modules/mongoose/examples/express/README.md
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
Mongoose + Express examples
|
||||
6
node_modules/mongoose/examples/express/connection-sharing/README.md
generated
vendored
Normal file
6
node_modules/mongoose/examples/express/connection-sharing/README.md
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
To run:
|
||||
|
||||
- Execute `npm install` from this directory
|
||||
- Execute `node app.js`
|
||||
- Navigate to `localhost:8000`
|
||||
18
node_modules/mongoose/examples/express/connection-sharing/app.js
generated
vendored
Normal file
18
node_modules/mongoose/examples/express/connection-sharing/app.js
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
var express = require('express')
|
||||
var mongoose = require('../../../lib')
|
||||
|
||||
var uri = 'mongodb://localhost/mongoose-shared-connection';
|
||||
global.db = mongoose.createConnection(uri);
|
||||
|
||||
var routes = require('./routes')
|
||||
|
||||
var app = express();
|
||||
app.get('/', routes.home);
|
||||
app.get('/insert', routes.insert);
|
||||
app.get('/name', routes.modelName);
|
||||
|
||||
app.listen(8000, function () {
|
||||
console.log('listening on http://localhost:8000');
|
||||
})
|
||||
|
||||
6
node_modules/mongoose/examples/express/connection-sharing/modelA.js
generated
vendored
Normal file
6
node_modules/mongoose/examples/express/connection-sharing/modelA.js
generated
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
var Schema = require('../../../lib').Schema;
|
||||
var mySchema = Schema({ name: String });
|
||||
|
||||
// db is global
|
||||
module.exports = db.model('MyModel', mySchema);
|
||||
14
node_modules/mongoose/examples/express/connection-sharing/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/express/connection-sharing/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "connection-sharing",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "ERROR: No README.md file found!",
|
||||
"main": "app.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "express": "3.1.1" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
20
node_modules/mongoose/examples/express/connection-sharing/routes.js
generated
vendored
Normal file
20
node_modules/mongoose/examples/express/connection-sharing/routes.js
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
var model = require('./modelA')
|
||||
|
||||
exports.home = function (req, res, next) {
|
||||
model.find(function (err, docs) {
|
||||
if (err) return next(err);
|
||||
res.send(docs);
|
||||
})
|
||||
}
|
||||
|
||||
exports.modelName = function (req, res) {
|
||||
res.send('my model name is ' + model.modelName);
|
||||
}
|
||||
|
||||
exports.insert = function (req, res, next) {
|
||||
model.create({ name: 'inserting ' + Date.now() }, function (err, doc) {
|
||||
if (err) return next(err);
|
||||
res.send(doc);
|
||||
})
|
||||
}
|
||||
22
node_modules/mongoose/examples/geospatial/geoJSONSchema.js
generated
vendored
Normal file
22
node_modules/mongoose/examples/geospatial/geoJSONSchema.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
// NOTE : This object must conform *precisely* to the geoJSON specification
|
||||
// you cannot embed a geoJSON doc inside a model or anything like that- IT
|
||||
// MUST BE VANILLA
|
||||
var LocationObject = new Schema({
|
||||
loc: {
|
||||
type: { type: String },
|
||||
coordinates: []
|
||||
}
|
||||
});
|
||||
// define the index
|
||||
LocationObject.index({ loc : '2dsphere' });
|
||||
|
||||
mongoose.model('Location', LocationObject);
|
||||
};
|
||||
49
node_modules/mongoose/examples/geospatial/geoJSONexample.js
generated
vendored
Normal file
49
node_modules/mongoose/examples/geospatial/geoJSONexample.js
generated
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./geoJSONSchema.js')();
|
||||
|
||||
var Location = mongoose.model('Location');
|
||||
|
||||
// define some dummy data
|
||||
// note: the type can be Point, LineString, or Polygon
|
||||
var data = [
|
||||
{ loc: { type: 'Point', coordinates: [-20.0, 5.0] }},
|
||||
{ loc: { type: 'Point', coordinates: [6.0, 10.0] }},
|
||||
{ loc: { type: 'Point', coordinates: [34.0, -50.0] }},
|
||||
{ loc: { type: 'Point', coordinates: [-100.0, 70.0] }},
|
||||
{ loc: { type: 'Point', coordinates: [38.0, 38.0] }}
|
||||
];
|
||||
|
||||
|
||||
mongoose.connect('mongodb://localhost/locations', function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
Location.on('index', function(err) {
|
||||
if (err) throw err;
|
||||
// create all of the dummy locations
|
||||
async.each(data, function (item, cb) {
|
||||
Location.create(item, cb);
|
||||
}, function (err) {
|
||||
if (err) throw err;
|
||||
// create the location we want to search for
|
||||
var coords = { type : 'Point', coordinates : [-5, 5] };
|
||||
// search for it
|
||||
Location.find({ loc : { $near : coords }}).limit(1).exec(function(err, res) {
|
||||
if (err) throw err;
|
||||
console.log("Closest to %s is %s", JSON.stringify(coords), res);
|
||||
cleanup();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Location.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
67
node_modules/mongoose/examples/geospatial/geospatial.js
generated
vendored
Normal file
67
node_modules/mongoose/examples/geospatial/geospatial.js
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./person.js')();
|
||||
|
||||
var Person = mongoose.model('Person');
|
||||
|
||||
// define some dummy data
|
||||
var data = [
|
||||
{ name : 'bill', age : 25, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 25)), gender : "Male",
|
||||
likes : ['movies', 'games', 'dogs'], loc : [0, 0]},
|
||||
{ name : 'mary', age : 30, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 30)), gender : "Female",
|
||||
likes : ['movies', 'birds', 'cats'], loc : [1, 1]},
|
||||
{ name : 'bob', age : 21, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 21)), gender : "Male",
|
||||
likes : ['tv', 'games', 'rabbits'], loc : [3, 3]},
|
||||
{ name : 'lilly', age : 26, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 26)), gender : "Female",
|
||||
likes : ['books', 'cats', 'dogs'], loc : [6, 6]},
|
||||
{ name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 1000)), gender : "Male",
|
||||
likes : ['glasses', 'wine', 'the night'], loc : [10, 10]},
|
||||
];
|
||||
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// create all of the dummy people
|
||||
async.each(data, function (item, cb) {
|
||||
Person.create(item, cb);
|
||||
}, function (err) {
|
||||
|
||||
// let's find the closest person to bob
|
||||
Person.find({ name : 'bob' }, function (err, res) {
|
||||
if (err) throw err;
|
||||
|
||||
res[0].findClosest(function (err, closest) {
|
||||
if (err) throw err;
|
||||
|
||||
console.log("%s is closest to %s", res[0].name, closest);
|
||||
|
||||
|
||||
// we can also just query straight off of the model. For more
|
||||
// information about geospatial queries and indexes, see
|
||||
// http://docs.mongodb.org/manual/applications/geospatial-indexes/
|
||||
var coords = [7,7];
|
||||
Person.find({ loc : { $nearSphere : coords }}).limit(1).exec(function(err, res) {
|
||||
console.log("Closest to %s is %s", coords, res);
|
||||
cleanup();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
14
node_modules/mongoose/examples/geospatial/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/geospatial/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "geospatial-example",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "deps for geospatial example",
|
||||
"main": "geospatial.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "async": "*" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
28
node_modules/mongoose/examples/geospatial/person.js
generated
vendored
Normal file
28
node_modules/mongoose/examples/geospatial/person.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date,
|
||||
gender: String,
|
||||
likes: [String],
|
||||
// define the geospatial field
|
||||
loc: { type : [Number], index: '2d' }
|
||||
});
|
||||
|
||||
// define a method to find the closest person
|
||||
PersonSchema.methods.findClosest = function(cb) {
|
||||
return this.model('Person').find({
|
||||
loc : { $nearSphere : this.loc },
|
||||
name : { $ne : this.name }
|
||||
}).limit(1).exec(cb);
|
||||
};
|
||||
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
40
node_modules/mongoose/examples/globalschemas/gs_example.js
generated
vendored
Normal file
40
node_modules/mongoose/examples/globalschemas/gs_example.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
|
||||
// import the global schema, this can be done in any file that needs the model
|
||||
require('./person.js')();
|
||||
|
||||
// grab the person model object
|
||||
var Person = mongoose.model("Person");
|
||||
|
||||
// connect to a server to do a quick write / read example
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function(err) {
|
||||
if (err) throw err;
|
||||
|
||||
Person.create({
|
||||
name : 'bill',
|
||||
age : 25,
|
||||
birthday : new Date().setFullYear((new Date().getFullYear() - 25))
|
||||
}, function (err, bill) {
|
||||
if (err) throw err;
|
||||
console.log("People added to db: %s", bill.toString());
|
||||
Person.find({}, function(err, people) {
|
||||
if (err) throw err;
|
||||
|
||||
people.forEach(function(person) {
|
||||
console.log("People in the db: %s", person.toString());
|
||||
});
|
||||
|
||||
// make sure to clean things up after we're done
|
||||
setTimeout(function () { cleanup(); }, 2000);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
15
node_modules/mongoose/examples/globalschemas/person.js
generated
vendored
Normal file
15
node_modules/mongoose/examples/globalschemas/person.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date
|
||||
});
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
61
node_modules/mongoose/examples/lean/lean.js
generated
vendored
Normal file
61
node_modules/mongoose/examples/lean/lean.js
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./person.js')();
|
||||
|
||||
var Person = mongoose.model('Person');
|
||||
|
||||
// define some dummy data
|
||||
var data = [
|
||||
{ name : 'bill', age : 25, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 25)), gender : "Male",
|
||||
likes : ['movies', 'games', 'dogs']},
|
||||
{ name : 'mary', age : 30, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 30)), gender : "Female",
|
||||
likes : ['movies', 'birds', 'cats']},
|
||||
{ name : 'bob', age : 21, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 21)), gender : "Male",
|
||||
likes : ['tv', 'games', 'rabbits']},
|
||||
{ name : 'lilly', age : 26, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 26)), gender : "Female",
|
||||
likes : ['books', 'cats', 'dogs']},
|
||||
{ name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 1000)), gender : "Male",
|
||||
likes : ['glasses', 'wine', 'the night']},
|
||||
];
|
||||
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// create all of the dummy people
|
||||
async.each(data, function (item, cb) {
|
||||
Person.create(item, cb);
|
||||
}, function (err) {
|
||||
|
||||
// lean queries return just plain javascript objects, not
|
||||
// MongooseDocuments. This makes them good for high performance read
|
||||
// situations
|
||||
|
||||
// when using .lean() the default is true, but you can explicitly set the
|
||||
// value by passing in a boolean value. IE. .lean(false)
|
||||
var q = Person.find({ age : { $lt : 1000 }}).sort('age').limit(2).lean();
|
||||
q.exec(function (err, results) {
|
||||
if (err) throw err;
|
||||
console.log("Are the results MongooseDocuments?: %s", results[0] instanceof mongoose.Document);
|
||||
|
||||
console.log(results);
|
||||
cleanup();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
14
node_modules/mongoose/examples/lean/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/lean/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "lean-example",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "deps for lean example",
|
||||
"main": "lean.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "async": "*" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
17
node_modules/mongoose/examples/lean/person.js
generated
vendored
Normal file
17
node_modules/mongoose/examples/lean/person.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date,
|
||||
gender: String,
|
||||
likes: [String]
|
||||
});
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
77
node_modules/mongoose/examples/mapreduce/mapreduce.js
generated
vendored
Normal file
77
node_modules/mongoose/examples/mapreduce/mapreduce.js
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./person.js')();
|
||||
|
||||
var Person = mongoose.model('Person');
|
||||
|
||||
// define some dummy data
|
||||
var data = [
|
||||
{ name : 'bill', age : 25, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 25)), gender : "Male" },
|
||||
{ name : 'mary', age : 30, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 30)), gender : "Female" },
|
||||
{ name : 'bob', age : 21, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 21)), gender : "Male" },
|
||||
{ name : 'lilly', age : 26, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 26)), gender : "Female" },
|
||||
{ name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 1000)), gender : "Male" },
|
||||
];
|
||||
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// create all of the dummy people
|
||||
async.each(data, function (item, cb) {
|
||||
Person.create(item, cb);
|
||||
}, function (err) {
|
||||
|
||||
// alright, simple map reduce example. We will find the total ages of each
|
||||
// gender
|
||||
|
||||
// create the options object
|
||||
var o = {};
|
||||
|
||||
o.map = function () {
|
||||
// in this function, 'this' refers to the current document being
|
||||
// processed. Return the (gender, age) tuple using emit()
|
||||
emit(this.gender, this.age);
|
||||
};
|
||||
|
||||
// the reduce function receives the array of ages that are grouped by the
|
||||
// id, which in this case is the gender
|
||||
o.reduce = function (id, ages) {
|
||||
return Array.sum(ages);
|
||||
};
|
||||
|
||||
// other options that can be specified
|
||||
|
||||
// o.query = { age : { $lt : 1000 }}; // the query object
|
||||
// o.limit = 3; // max number of documents
|
||||
// o.keeptemp = true; // default is false, specifies whether to keep temp data
|
||||
// o.finalize = someFunc; // function called after reduce
|
||||
// o.scope = {}; // the scope variable exposed to map/reduce/finalize
|
||||
// o.jsMode = true; // default is false, force execution to stay in JS
|
||||
o.verbose = true; // default is false, provide stats on the job
|
||||
// o.out = {}; // objects to specify where output goes, by default is
|
||||
// returned, but can also be stored in a new collection
|
||||
// see: http://mongoosejs.com/docs/api.html#model_Model.mapReduce
|
||||
Person.mapReduce(o, function (err, results, stats) {
|
||||
console.log("map reduce took %d ms", stats.processtime);
|
||||
console.log(results);
|
||||
cleanup();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
14
node_modules/mongoose/examples/mapreduce/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/mapreduce/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "map-reduce-example",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "deps for map reduce example",
|
||||
"main": "mapreduce.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "async": "*" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
16
node_modules/mongoose/examples/mapreduce/person.js
generated
vendored
Normal file
16
node_modules/mongoose/examples/mapreduce/person.js
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date,
|
||||
gender: String
|
||||
});
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
135
node_modules/mongoose/examples/population/population-across-three-collections.js
generated
vendored
Normal file
135
node_modules/mongoose/examples/population/population-across-three-collections.js
generated
vendored
Normal file
@@ -0,0 +1,135 @@
|
||||
|
||||
var assert = require('assert')
|
||||
var mongoose = require('../../lib');
|
||||
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();
|
||||
});
|
||||
}
|
||||
95
node_modules/mongoose/examples/population/population-basic.js
generated
vendored
Normal file
95
node_modules/mongoose/examples/population/population-basic.js
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
|
||||
var mongoose = require('../../lib')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var 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();
|
||||
})
|
||||
})
|
||||
}
|
||||
101
node_modules/mongoose/examples/population/population-of-existing-doc.js
generated
vendored
Normal file
101
node_modules/mongoose/examples/population/population-of-existing-doc.js
generated
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
|
||||
var mongoose = require('../../lib')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var 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/ })
|
||||
.exec(function (err, ocinara) {
|
||||
if (err) return done(err);
|
||||
|
||||
console.log('"%s" console _id: %s', ocinara.name, ocinara.consoles[0]);
|
||||
|
||||
// population of existing document
|
||||
ocinara.populate('consoles', function (err) {
|
||||
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();
|
||||
})
|
||||
})
|
||||
}
|
||||
112
node_modules/mongoose/examples/population/population-of-multiple-existing-docs.js
generated
vendored
Normal file
112
node_modules/mongoose/examples/population/population-of-multiple-existing-docs.js
generated
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
var mongoose = require('../../lib')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var 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'
|
||||
}, {
|
||||
name: 'Super Nintendo'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'August 23, 1991'
|
||||
}, function (err, nintendo64, superNintendo) {
|
||||
if (err) return done(err);
|
||||
|
||||
Game.create({
|
||||
name: 'Legend of Zelda: Ocarina of Time'
|
||||
, developer: 'Nintendo'
|
||||
, released: new Date('November 21, 1998')
|
||||
, consoles: [nintendo64]
|
||||
}, {
|
||||
name: 'Mario Kart'
|
||||
, developer: 'Nintendo'
|
||||
, released: 'September 1, 1992'
|
||||
, consoles: [superNintendo]
|
||||
}, function (err) {
|
||||
if (err) return done(err);
|
||||
example();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Game
|
||||
.find({})
|
||||
.exec(function (err, games) {
|
||||
if (err) return done(err);
|
||||
|
||||
console.log('found %d games', games.length);
|
||||
|
||||
var options = { path: 'consoles', select: 'name released -_id' };
|
||||
Game.populate(games, options, function (err, games) {
|
||||
if (err) return done(err);
|
||||
|
||||
games.forEach(function (game) {
|
||||
console.log(
|
||||
'"%s" was released for the %s on %s'
|
||||
, game.name
|
||||
, game.consoles[0].name
|
||||
, game.released.toLocaleDateString());
|
||||
})
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
Console.remove(function () {
|
||||
Game.remove(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
})
|
||||
}
|
||||
124
node_modules/mongoose/examples/population/population-options.js
generated
vendored
Normal file
124
node_modules/mongoose/examples/population/population-options.js
generated
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
|
||||
var mongoose = require('../../lib')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var 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'
|
||||
}, {
|
||||
name: 'Super Nintendo'
|
||||
, manufacturer: 'Nintendo'
|
||||
, released: 'August 23, 1991'
|
||||
}, {
|
||||
name: 'XBOX 360'
|
||||
, manufacturer: 'Microsoft'
|
||||
, released: 'November 22, 2005'
|
||||
}, function (err, nintendo64, superNintendo, xbox360) {
|
||||
if (err) return done(err);
|
||||
|
||||
Game.create({
|
||||
name: 'Legend of Zelda: Ocarina of Time'
|
||||
, developer: 'Nintendo'
|
||||
, released: new Date('November 21, 1998')
|
||||
, consoles: [nintendo64]
|
||||
}, {
|
||||
name: 'Mario Kart'
|
||||
, developer: 'Nintendo'
|
||||
, released: 'September 1, 1992'
|
||||
, consoles: [superNintendo]
|
||||
}, {
|
||||
name: 'Perfect Dark Zero'
|
||||
, developer: 'Rare'
|
||||
, released: 'November 17, 2005'
|
||||
, consoles: [xbox360]
|
||||
}, function (err) {
|
||||
if (err) return done(err);
|
||||
example();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Population
|
||||
*/
|
||||
|
||||
function example () {
|
||||
Game
|
||||
.find({})
|
||||
.populate({
|
||||
path: 'consoles'
|
||||
, match: { manufacturer: 'Nintendo' }
|
||||
, select: 'name'
|
||||
, options: { comment: 'population' }
|
||||
})
|
||||
.exec(function (err, games) {
|
||||
if (err) return done(err);
|
||||
|
||||
games.forEach(function (game) {
|
||||
console.log(
|
||||
'"%s" was released for the %s on %s'
|
||||
, game.name
|
||||
, game.consoles.length ? game.consoles[0].name : '??'
|
||||
, game.released.toLocaleDateString());
|
||||
})
|
||||
|
||||
return done();
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up
|
||||
*/
|
||||
|
||||
function done (err) {
|
||||
if (err) console.error(err);
|
||||
Console.remove(function () {
|
||||
Game.remove(function () {
|
||||
mongoose.disconnect();
|
||||
})
|
||||
})
|
||||
}
|
||||
96
node_modules/mongoose/examples/population/population-plain-objects.js
generated
vendored
Normal file
96
node_modules/mongoose/examples/population/population-plain-objects.js
generated
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
var mongoose = require('../../lib')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
console.log('Running mongoose version %s', mongoose.version);
|
||||
|
||||
/**
|
||||
* Console schema
|
||||
*/
|
||||
|
||||
var consoleSchema = Schema({
|
||||
name: String
|
||||
, manufacturer: String
|
||||
, released: Date
|
||||
})
|
||||
var Console = mongoose.model('Console', consoleSchema);
|
||||
|
||||
/**
|
||||
* Game schema
|
||||
*/
|
||||
|
||||
var gameSchema = Schema({
|
||||
name: String
|
||||
, developer: String
|
||||
, released: Date
|
||||
, consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
|
||||
})
|
||||
var 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')
|
||||
.lean() // just return plain objects, not documents wrapped by mongoose
|
||||
.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();
|
||||
})
|
||||
})
|
||||
}
|
||||
14
node_modules/mongoose/examples/promises/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/promises/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "promise-example",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "deps for promise example",
|
||||
"main": "promise.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "async": "*" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
15
node_modules/mongoose/examples/promises/person.js
generated
vendored
Normal file
15
node_modules/mongoose/examples/promises/person.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date
|
||||
});
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
70
node_modules/mongoose/examples/promises/promise.js
generated
vendored
Normal file
70
node_modules/mongoose/examples/promises/promise.js
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./person.js')();
|
||||
|
||||
var Person = mongoose.model('Person');
|
||||
|
||||
// define some dummy data
|
||||
var data = [
|
||||
{ name : 'bill', age : 25, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 25)) },
|
||||
{ name : 'mary', age : 30, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 30)) },
|
||||
{ name : 'bob', age : 21, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 21)) },
|
||||
{ name : 'lilly', age : 26, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 26)) },
|
||||
{ name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 1000)) },
|
||||
];
|
||||
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// create all of the dummy people
|
||||
async.each(data, function (item, cb) {
|
||||
Person.create(item, cb);
|
||||
}, function (err) {
|
||||
|
||||
// create a promise (get one from the query builder)
|
||||
var prom = Person.find({age : { $lt : 1000 }}).exec();
|
||||
|
||||
// add a callback on the promise. This will be called on both error and
|
||||
// complete
|
||||
prom.addBack(function () { console.log("completed"); });
|
||||
|
||||
// add a callback that is only called on complete (success) events
|
||||
prom.addCallback(function () { console.log("Successful Completion!"); });
|
||||
|
||||
// add a callback that is only called on err (rejected) events
|
||||
prom.addErrback(function () { console.log("Fail Boat"); });
|
||||
|
||||
// you can chain things just like in the promise/A+ spec
|
||||
// note: each then() is returning a new promise, so the above methods
|
||||
// that we defined will all fire after the initial promise is fulfilled
|
||||
prom.then(function (people) {
|
||||
|
||||
// just getting the stuff for the next query
|
||||
var ids = people.map(function (p) {
|
||||
return p._id;
|
||||
});
|
||||
|
||||
// return the next promise
|
||||
return Person.find({ _id : { $nin : ids }}).exec();
|
||||
}).then(function (oldest) {
|
||||
console.log("Oldest person is: %s", oldest);
|
||||
}).then(cleanup);
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
14
node_modules/mongoose/examples/querybuilder/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/querybuilder/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "query-builder-example",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "deps for query builder example",
|
||||
"main": "querybuilder.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "async": "*" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
15
node_modules/mongoose/examples/querybuilder/person.js
generated
vendored
Normal file
15
node_modules/mongoose/examples/querybuilder/person.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date
|
||||
});
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
65
node_modules/mongoose/examples/querybuilder/querybuilder.js
generated
vendored
Normal file
65
node_modules/mongoose/examples/querybuilder/querybuilder.js
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./person.js')();
|
||||
|
||||
var Person = mongoose.model('Person');
|
||||
|
||||
// define some dummy data
|
||||
var data = [
|
||||
{ name : 'bill', age : 25, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 25)) },
|
||||
{ name : 'mary', age : 30, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 30)) },
|
||||
{ name : 'bob', age : 21, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 21)) },
|
||||
{ name : 'lilly', age : 26, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 26)) },
|
||||
{ name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 1000)) },
|
||||
];
|
||||
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// create all of the dummy people
|
||||
async.each(data, function (item, cb) {
|
||||
Person.create(item, cb);
|
||||
}, function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// when querying data, instead of providing a callback, you can instead
|
||||
// leave that off and get a query object returned
|
||||
var query = Person.find({ age : { $lt : 1000 }});
|
||||
|
||||
// this allows you to continue applying modifiers to it
|
||||
query.sort('birthday');
|
||||
query.select('name');
|
||||
|
||||
// you can chain them together as well
|
||||
// a full list of methods can be found:
|
||||
// http://mongoosejs.com/docs/api.html#query-js
|
||||
query.where('age').gt(21);
|
||||
|
||||
// finally, when ready to execute the query, call the exec() function
|
||||
query.exec(function (err, results) {
|
||||
if (err) throw err;
|
||||
|
||||
console.log(results);
|
||||
|
||||
cleanup();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
14
node_modules/mongoose/examples/replicasets/package.json
generated
vendored
Normal file
14
node_modules/mongoose/examples/replicasets/package.json
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "replica-set-example",
|
||||
"private": "true",
|
||||
"version": "0.0.0",
|
||||
"description": "deps for replica set example",
|
||||
"main": "querybuilder.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"dependencies": { "async": "*" },
|
||||
"repository": "",
|
||||
"author": "",
|
||||
"license": "BSD"
|
||||
}
|
||||
15
node_modules/mongoose/examples/replicasets/person.js
generated
vendored
Normal file
15
node_modules/mongoose/examples/replicasets/person.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date
|
||||
});
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
53
node_modules/mongoose/examples/replicasets/replica-sets.js
generated
vendored
Normal file
53
node_modules/mongoose/examples/replicasets/replica-sets.js
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
// import async to make control flow simplier
|
||||
var async = require('async');
|
||||
|
||||
// import the rest of the normal stuff
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
require('./person.js')();
|
||||
|
||||
var Person = mongoose.model('Person');
|
||||
|
||||
// define some dummy data
|
||||
var data = [
|
||||
{ name : 'bill', age : 25, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 25)) },
|
||||
{ name : 'mary', age : 30, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 30)) },
|
||||
{ name : 'bob', age : 21, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 21)) },
|
||||
{ name : 'lilly', age : 26, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 26)) },
|
||||
{ name : 'alucard', age : 1000, birthday : new Date().setFullYear((new
|
||||
Date().getFullYear() - 1000)) },
|
||||
];
|
||||
|
||||
|
||||
// to connect to a replica set, pass in the comma delimited uri and optionally
|
||||
// any connection options such as the rs_name.
|
||||
var opts = {
|
||||
replSet : { rs_name : "rs0" }
|
||||
};
|
||||
mongoose.connect('mongodb://localhost:27018/persons,localhost:27019,localhost:27020', opts, function (err) {
|
||||
if (err) throw err;
|
||||
|
||||
// create all of the dummy people
|
||||
async.each(data, function (item, cb) {
|
||||
Person.create(item, cb);
|
||||
}, function (err) {
|
||||
|
||||
// create and delete some data
|
||||
var prom = Person.find({age : { $lt : 1000 }}).exec();
|
||||
|
||||
prom.then(function (people) {
|
||||
console.log("young people: %s", people);
|
||||
}).then(cleanup);
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
102
node_modules/mongoose/examples/schema/schema.js
generated
vendored
Normal file
102
node_modules/mongoose/examples/schema/schema.js
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var mongoose = require('../../lib')
|
||||
, Schema = mongoose.Schema;
|
||||
|
||||
/**
|
||||
* Schema definition
|
||||
*/
|
||||
|
||||
// recursive embedded-document schema
|
||||
|
||||
var Comment = new Schema();
|
||||
|
||||
Comment.add({
|
||||
title : { type: String, index: true }
|
||||
, date : Date
|
||||
, body : String
|
||||
, comments : [Comment]
|
||||
});
|
||||
|
||||
var BlogPost = new Schema({
|
||||
title : { type: String, index: true }
|
||||
, slug : { type: String, lowercase: true, trim: true }
|
||||
, date : Date
|
||||
, buf : Buffer
|
||||
, comments : [Comment]
|
||||
, creator : Schema.ObjectId
|
||||
});
|
||||
|
||||
var Person = new Schema({
|
||||
name: {
|
||||
first: String
|
||||
, last : String
|
||||
}
|
||||
, email: { type: String, required: true, index: { unique: true, sparse: true } }
|
||||
, alive: Boolean
|
||||
});
|
||||
|
||||
/**
|
||||
* Accessing a specific schema type by key
|
||||
*/
|
||||
|
||||
BlogPost.path('date')
|
||||
.default(function(){
|
||||
return new Date()
|
||||
})
|
||||
.set(function(v){
|
||||
return v == 'now' ? new Date() : v;
|
||||
});
|
||||
|
||||
/**
|
||||
* Pre hook.
|
||||
*/
|
||||
|
||||
BlogPost.pre('save', function(next, done){
|
||||
emailAuthor(done); // some async function
|
||||
next();
|
||||
});
|
||||
|
||||
/**
|
||||
* Methods
|
||||
*/
|
||||
|
||||
BlogPost.methods.findCreator = function (callback) {
|
||||
return this.db.model('Person').findById(this.creator, callback);
|
||||
}
|
||||
|
||||
BlogPost.statics.findByTitle = function (title, callback) {
|
||||
return this.find({ title: title }, callback);
|
||||
}
|
||||
|
||||
BlogPost.methods.expressiveQuery = function (creator, date, callback) {
|
||||
return this.find('creator', creator).where('date').gte(date).run(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plugins
|
||||
*/
|
||||
|
||||
function slugGenerator (options){
|
||||
options = options || {};
|
||||
var key = options.key || 'title';
|
||||
|
||||
return function slugGenerator(schema){
|
||||
schema.path(key).set(function(v){
|
||||
this.slug = v.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/-+/g, '');
|
||||
return v;
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
BlogPost.plugin(slugGenerator());
|
||||
|
||||
/**
|
||||
* Define model.
|
||||
*/
|
||||
|
||||
mongoose.model('BlogPost', BlogPost);
|
||||
mongoose.model('Person', Person);
|
||||
27
node_modules/mongoose/examples/schema/storing-schemas-as-json/index.js
generated
vendored
Normal file
27
node_modules/mongoose/examples/schema/storing-schemas-as-json/index.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
// modules
|
||||
var mongoose = require('../../../lib')
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// parse json
|
||||
var raw = require('./schema.json');
|
||||
|
||||
// create a schema
|
||||
var timeSignatureSchema = Schema(raw);
|
||||
|
||||
// compile the model
|
||||
var TimeSignature = mongoose.model('TimeSignatures', timeSignatureSchema);
|
||||
|
||||
// create a TimeSignature document
|
||||
var threeFour = new TimeSignature({
|
||||
count: 3
|
||||
, unit: 4
|
||||
, description: "3/4"
|
||||
, additive: false
|
||||
, created: new Date
|
||||
, links: ["http://en.wikipedia.org/wiki/Time_signature"]
|
||||
, user_id: "518d31a0ef32bbfa853a9814"
|
||||
});
|
||||
|
||||
// print its description
|
||||
console.log(threeFour)
|
||||
9
node_modules/mongoose/examples/schema/storing-schemas-as-json/schema.json
generated
vendored
Normal file
9
node_modules/mongoose/examples/schema/storing-schemas-as-json/schema.json
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"count": "number",
|
||||
"unit": "number",
|
||||
"description": "string",
|
||||
"links": ["string"],
|
||||
"created": "date",
|
||||
"additive": "boolean",
|
||||
"user_id": "ObjectId"
|
||||
}
|
||||
21
node_modules/mongoose/examples/statics/person.js
generated
vendored
Normal file
21
node_modules/mongoose/examples/statics/person.js
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
// import the necessary modules
|
||||
var mongoose = require('../../lib');
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
// create an export function to encapsulate the model creation
|
||||
module.exports = function() {
|
||||
// define schema
|
||||
var PersonSchema = new Schema({
|
||||
name : String,
|
||||
age : Number,
|
||||
birthday : Date
|
||||
});
|
||||
|
||||
// define a static
|
||||
PersonSchema.statics.findPersonByName = function (name, cb) {
|
||||
this.find({ name : new RegExp(name, 'i') }, cb);
|
||||
};
|
||||
|
||||
mongoose.model('Person', PersonSchema);
|
||||
};
|
||||
38
node_modules/mongoose/examples/statics/statics.js
generated
vendored
Normal file
38
node_modules/mongoose/examples/statics/statics.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
var mongoose = require('../../lib');
|
||||
|
||||
|
||||
// import the schema
|
||||
require('./person.js')();
|
||||
|
||||
// grab the person model object
|
||||
var Person = mongoose.model("Person");
|
||||
|
||||
// connect to a server to do a quick write / read example
|
||||
|
||||
mongoose.connect('mongodb://localhost/persons', function(err) {
|
||||
if (err) throw err;
|
||||
|
||||
Person.create({
|
||||
name : 'bill',
|
||||
age : 25,
|
||||
birthday : new Date().setFullYear((new Date().getFullYear() - 25))
|
||||
}, function (err, bill) {
|
||||
if (err) throw err;
|
||||
console.log("People added to db: %s", bill.toString());
|
||||
|
||||
// using the static
|
||||
Person.findPersonByName('bill', function(err, result) {
|
||||
if (err) throw err;
|
||||
|
||||
console.log(result);
|
||||
cleanup();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function cleanup() {
|
||||
Person.remove(function() {
|
||||
mongoose.disconnect();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user