mongodb_connect.js
708 Bytes
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
var mongoose = require('mongoose');
mongoose.connect('mongodb://username:pwd@host/dbname');
var db = mongoose.connection;
//연결실패
db.on('error', function(){
console.log('Connection Failed!');
});
//연결 성공
db.once('open', function() {
console.log('Connected!');
});
var testSchema = mongoose.Schema
({
name: String
});
var TestModel = mongoose.model("TestModel", testSchema);
/*
var test = new TestModel({ name: "test" });
test.save(function(err, test)
{
if(err){console.log(err);}
else{console.log("Success!");}
console.log("ok4");
});
*/
TestModel.find(function(err, test){
if(err){console.log(err);}
else{
console.log(test);
}
});
db.close()