Last active
December 19, 2015 22:48
-
-
Save mchapman/6029576 to your computer and use it in GitHub Desktop.
Mongoose question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var assert = require('assert') | |
console.log('\n==========='); | |
console.log(' mongoose version: %s', mongoose.version); | |
console.log('========\n\n'); | |
mongoose.set('debug', true); | |
mongoose.connect('localhost', 'dates'); | |
mongoose.connection.on('error', function () { | |
console.error('connection error', arguments); | |
}); | |
// user model | |
var userSchema = Schema({ name: 'string' , dob: Date}); | |
var User = mongoose.model('User', userSchema); | |
mongoose.connection.on('open', function () { | |
User.create([{ name: 'mark' , dob: new Date(1970,0,1)}, { name: 'ann' , dob: new Date(1972,0,1)}], function(err){ | |
if (err) return done(err); | |
// Find | |
var findCommand = '{"dob":"1970-01-01"}' | |
User.find(JSON.parse(findCommand),function(err,result){ | |
if (err) return done(err); | |
console.log("Find: "+JSON.stringify(result)); | |
// Aggregate | |
// var aggCommand = '[{"$match":{"name":"mark"}},{"$project":{"name":1}}]' // works fine | |
var aggCommand = '[{"$match":{"dob":"1970-01-01"}},{"$project":{"name":1}}]' // returns empty array | |
User.aggregate(JSON.parse(aggCommand),function(err,result){ | |
if (err) return done(err); | |
console.log("Aggregate: "+JSON.stringify(result)); | |
done(); | |
}) | |
}) | |
}) | |
}); | |
function done (err) { | |
if (err) console.error(err.stack); | |
mongoose.connection.db.dropDatabase(function () { | |
mongoose.connection.close(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment