文章目录
  1. 1. 链接数据库
  2. 2. 插入文档
  3. 3. 修改文档
  4. 4. 删除
  5. 5. 查询

链接数据库

1
2
3
4
5
6
7
8
9
10
var MongoClient = require('mongodb').MongoClient,
assert = require('assert'); // 断言

var url = 'mongodb://localhost:27017/myproject'
MongoClient.connect(url,function(err,db)){
assert.equal(null,err); // 如果err == null 继续执行
console.log('Connected correctly to server');

db.close();
}

插入文档

1
2
3
4
5
6
7
8
9
10
var insertDocument = function(db,callback) {
var collection = db.collection('document');
collection.insert([{a:1},{a:2},{a:3}],function(err,result){
assert.equal(err, null);
assert.equal(3, result.result.n);
assert.equal(3, result.ops.length);
console.log("Inserted 3 documents into the document collection");
callback(result);
});
}

可以在连接时增加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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");

insertDocuments(db, function() {
db.close();
});
});

修改文档

1
2
3
4
5
6
7
8
9
10
11
12
var updateDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Update document where a is 2, set b equal to 1
collection.update({ a : 2 }
, { $set: { b : 1 } }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Updated the document with the field a equal to 2");
callback(result);
});
}

也可以在连接时回调

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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");

insertDocuments(db, function() {
updateDocument(db, function() {
db.close();
});
});
});

删除

1
2
3
4
5
6
7
8
9
10
11
var removeDocument = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Insert some documents
collection.remove({ a : 3 }, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log("Removed the document with the field a equal to 3");
callback(result);
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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");

insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
db.close();
});
});
});
});

查询

1
2
3
4
5
6
7
8
9
10
11
12
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Find some documents
collection.find({}).toArray(function(err, docs) {
assert.equal(err, null);
assert.equal(2, docs.length);
console.log("Found the following records");
console.dir(docs)
callback(docs);
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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");

insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
findDocuments(db, function() {
db.close();
});
});
});
});
});
文章目录
  1. 1. 链接数据库
  2. 2. 插入文档
  3. 3. 修改文档
  4. 4. 删除
  5. 5. 查询