Created
June 22, 2014 17:43
-
-
Save harpreetkhalsagtbit/2860b655824bb6f20235 to your computer and use it in GitHub Desktop.
Save after push to a refrence model
This file contains hidden or 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 async = require('async'); | |
global._clients = {}; | |
var mongoose = require('mongoose'); | |
global._mongoose = mongoose; | |
var conf = { | |
db: { | |
db: 'gendocx', | |
host: 'localhost', | |
port: 27017, | |
// optional, default: 27017 | |
username: '', | |
// username, optional | |
password: '', | |
// password, optional | |
collection: 'mySessions' // optional, default: sessions | |
}, | |
secret: '076ee61d63aa10a125ea872411e433b9' | |
}; | |
var dbUrl = 'mongodb://'; | |
dbUrl += conf.db.username + ':' + conf.db.password + '@'; | |
dbUrl += conf.db.host + ':' + conf.db.port; | |
dbUrl += '/' + conf.db.db; | |
mongoose.connect(dbUrl); | |
Schema = mongoose.Schema | |
mongoose.connection.on('error', function(data) { | |
console.log('err', data) | |
}); | |
mongoose.connection.on('open', function() { | |
console.log('MongoDB connected') | |
}); | |
var ClientSchema = new Schema({ | |
email: String, | |
name: String, | |
nick: String, | |
domains: [{ type: Schema.Types.ObjectId, ref: 'Domain' }] | |
}); | |
var DomainSchema = new Schema({ | |
name: String | |
}); | |
var clientTable = mongoose.model('client', ClientSchema); | |
var domainTable = mongoose.model('Domain', DomainSchema); | |
// var newClient = new clientTable({ | |
// email: '[email protected]', | |
// name:'harpreet', | |
// nick:'har', | |
// domains:[] | |
// }); | |
// newClient.save(function(err, result) { | |
// console.log(result) | |
// }) | |
clientTable.findOne({ | |
name:'harpreet' | |
}, function(err, clientRes) { | |
console.log(clientRes) | |
var newDomain = new domainTable({ | |
name:'stack.com', | |
}); | |
newDomain.save(function(err, domainRes) { | |
console.log(domainRes) | |
clientRes.domains.push(domainRes._id); | |
clientRes.save(function(err, finalResult) { | |
console.log('finalResult', finalResult) | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output