var express = require('express');
////
var mongo = require('mongodb');
var Server = mongo.Server;
var Db = mongo.Db;
var server = new Server('ds013898.mlab.com',13898, {auto_reconnect : true});
var db = new Db('forclass', server);
db.open(function(err, client) {
client.authenticate('forclass1', 'test123', function(err, success) {
if(success){
console.log("connect success")
}else{
console.log("client.authenticate error")
};
});
});
////
var app = express();
app.listen(8080);
開始操作資料庫
Relation DataBase MongoDB
------------------------------------------------------------
資料庫(Database) DataBase
資料表(Table) Collection
資料(Record/Row) Document
欄位(Column) Field
主索引(PK) _id
function function ( )
stored procedure mapreduce
var express = require('express');
////
var mongo = require('mongodb');
var Server = mongo.Server;
var Db = mongo.Db;
var server = new Server('ds013898.mlab.com',13898, {auto_reconnect : true});
var db = new Db('forclass', server);
db.open(function(err, client) {
client.authenticate('forclass1', 'test123', function(err, success) {
if(success){
console.log("connect success")
var cursor = db.collection('apple').find();
cursor.each(function(err, doc) {
console.log(doc);
db.close();
});
}else{
console.log("client.authenticate error")
};
});
});
////
var app = express();
app.listen(8080);
2.如果在find(),裡面放入參數,會query出所有符合的document
var cursor = db.collection('apple').find({ "borough": "Bronx" });
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
db.close();
});
var kitty = new Cat({ name: 'Zildjian', friends: ['tom', 'jerry']});
kitty.age = 3;
4.使用save才真的存入
kitty.save(function (err) {
if (err) // ...
console.log('meow');
});
ps:如果存入資料的欄位不在schema內則不會存入
ps:如省略某些欄位沒寫,則不會顯示,亦可正常存入
var mongoose = require('mongoose');
mongoose.connect('mongodb://forclass1:test123@ds013898.mlab.com:13898/forclass');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("connect");
});
var Cat = mongoose.model('Cat', {
name: String,
friends: [String],
age: Number,
});
var kitty = new Cat({ name: 'Zildjian', friends: ['tom', 'jerry']});
kitty.age = 3;
//使用save方法後才會存入
kitty.save(function (err) {
if (err) // ...
console.log('meow');
});
使用Promise
var User = mongoose.model('ac', new mongoose.Schema({
name:{type: String, unique: true},
password:String
}));
var list = new User({name:"s", password:"123456"});
list.save()
.then(a => console.log(a))
.catch(err => console.log(err));
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
完整版
var mongoose = require('mongoose');
mongoose.connect('mongodb://forclass1:test123@ds013898.mlab.com:13898/forclass');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("connect");
});
var kittySchema = mongoose.Schema({
name: String
});
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
//////////method要定義在model實例化之前
var Kitten = mongoose.model('cats', kittySchema);
var fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak(); // "Meow name is fluffy"