var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; console.log("資料庫已建立!"); db.close(); });
執行結果如下:
1 2 3
$ node test.js (node:4753) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 資料庫已建立!
建立集合
使用createCollection()方法來建立集合:
1 2 3 4 5 6 7 8 9 10 11 12 13
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) { if (err) throw err; console.log('資料庫已建立'); var dbase = db.db("jjtest"); dbase.createCollection('site', function (err, res) { if (err) throw err; console.log("建立集合!"); db.close(); }); });
執行結果如下:
1 2 3 4
$ node test.js (node:4861) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 資料庫已建立 建立集合!
資料庫操作 (CURD)
與MySQL不同的是MongoDB會自動建立資料庫和集合,所以使用前我們不需要手動去建立。
插入資料
1 2 3 4 5 6 7 8 9 10 11 12 13
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); var myobj = { name: "J.J.'s blogs", url: "morosedog.gitlab.io" }; dbo.collection("site").insertOne(myobj, function(err, res) { if (err) throw err; console.log("資料插入成功"); db.close(); }); });
執行結果如下:
1 2 3
$ node test.js (node:4872) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 資料插入成功
MongoDB客戶端:
1 2 3 4 5 6 7 8 9 10 11 12
> show dbs admin 0.000GB config 0.000GB jjtest 0.000GB local 0.000GB > use jjtest switched to db jjtest > show tables site > db.site.find() { "_id" : ObjectId("5e44ef0cda14ed1308971ca7"), "name" : "J.J.'s blogs", "url" : "morosedog.gitlab.io" } >
$ node test.js (node:4977) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 插入的資料數量為: 3
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); dbo.collection("site"). find({}).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
執行結果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
$ node test.js (node:4989) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. [ { _id: 5e44f141b5d2871371235873, name: "J.J.'s blogs", url: 'morosedog.gitlab.io' }, { _id: 5e44f141b5d2871371235874, name: 'Google', url: 'https://www.google.com' }, { _id: 5e44f141b5d2871371235875, name: 'Facebook', url: 'https://zh-tw.facebook.com' } ]
查詢條件為”J.J.’s blogs”:
1 2 3 4 5 6 7 8 9 10 11 12 13
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); var whereStr = {"name":'J.J.\'s blogs'}; dbo.collection("site").find(whereStr).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
執行結果如下:
1 2 3 4 5 6 7 8 9
$ node test.js (node:5011) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. [ { _id: 5e44f141b5d2871371235873, name: "J.J.'s blogs", url: 'morosedog.gitlab.io' } ]
更新資料
1 2 3 4 5 6 7 8 9 10 11 12 13 14
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); var whereStr = {"name":'J.J.\'s blogs'}; var updateStr = {$set: { "url" : "https://morosedog.gitlab.io/" }}; dbo.collection("site").updateOne(whereStr, updateStr, function(err, res) { if (err) throw err; console.log("資料更新成功"); db.close(); }); });
執行結果如下:
1 2 3
$ node test.js (node:5028) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 資料更新成功
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); var whereStr = {"name":'J.J.\'s blogs'}; var updateStr = {$set: { "url" : "https://morosedog.gitlab.io/" }}; dbo.collection("site").updateMany(whereStr, updateStr, function(err, res) { if (err) throw err; console.log(res.result.nModified + " 條資料被更新"); db.close(); }); });
註:更新多筆資料就不特別示範了。
刪除資料
1 2 3 4 5 6 7 8 9 10 11 12 13
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest";
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); var whereStr = {"name":'J.J.\'s blogs'}; dbo.collection("site").deleteOne(whereStr, function(err, obj) { if (err) throw err; console.log("資料刪除成功"); db.close(); }); });
執行結果如下:
1 2 3
$ node test.js (node:5074) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 資料刪除成功
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest";
MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); var whereStr = {"name":'J.J.\'s blogs'}; dbo.collection("site").deleteMany(whereStr, function(err, obj) { if (err) throw err; console.log(obj.result.n + " 條資料被刪除"); db.close(); }); });
註:刪除多筆資料就不特別示範了。
刪除集合
使用drop()方法來刪除集合。
1 2 3 4 5 6 7 8 9 10 11 12
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); dbo.collection("site").drop(function(err, delOK) { if (err) throw err; if (delOK) console.log("集合已刪除"); db.close(); }); });
執行結果如下:
1 2 3
$ node test.js (node:5086) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. 集合已刪除
MongoDB客戶端:
1 2 3 4 5 6 7 8
> use jjtest switched to db jjtest > show tables site > > > show tables >
排序
使用sort()方法,該方法接受一個參數,規定是升序(1)還是降序(-1)。
1 2
{ type: 1 } // 按 type 字段升序 { type: -1 } // 按 type 字段降序
$ node test.js (node:5121) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. [ { _id: 5e44f9b0e8a4cb13e7650b40, name: "J.J.'s blogs", url: 'morosedog.gitlab.io', type: 1 }, { _id: 5e44f9b0e8a4cb13e7650b41, name: 'Google', url: 'https://www.google.com', type: 2 }, { _id: 5e44f9b0e8a4cb13e7650b42, name: 'Facebook', url: 'https://zh-tw.facebook.com', type: 3 } ]
按type降序排列。
1 2 3 4 5 6 7 8 9 10 11 12 13
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); var mysort = { type: -1 }; dbo.collection("site").find().sort(mysort).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
$ node test.js (node:5126) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. [ { _id: 5e44f9b0e8a4cb13e7650b42, name: 'Facebook', url: 'https://zh-tw.facebook.com', type: 3 }, { _id: 5e44f9b0e8a4cb13e7650b41, name: 'Google', url: 'https://www.google.com', type: 2 }, { _id: 5e44f9b0e8a4cb13e7650b40, name: "J.J.'s blogs", url: 'morosedog.gitlab.io', type: 1 } ]
查詢分頁
使用limit()方法,該方法只接受一個參數,指定了返回的條數。
1 2 3 4 5 6 7 8 9 10 11 12
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); dbo.collection("site").find().limit(2).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
執行結果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ node test.js (node:5172) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. [ { _id: 5e44f9b0e8a4cb13e7650b40, name: "J.J.'s blogs", url: 'morosedog.gitlab.io', type: 1 }, { _id: 5e44f9b0e8a4cb13e7650b41, name: 'Google', url: 'https://www.google.com', type: 2 } ]
指定跳過的條數,可以使用skip()方法。
1 2 3 4 5 6 7 8 9 10 11 12 13
var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/jjtest"; MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) { if (err) throw err; var dbo = db.db("jjtest"); // 跳過前面兩條資料,讀取兩條資料 dbo.collection("site").find().skip(2).limit(2).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
執行結果如下:
1 2 3 4 5 6 7 8 9 10
$ node test.js (node:5177) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. [ { _id: 5e44f9b0e8a4cb13e7650b42, name: 'Facebook', url: 'https://zh-tw.facebook.com', type: 3 } ]