標籤:ups 隨機 collect .sh npm 部落格 www. install 回調
Node.js串連MongoDB
一些介紹
mongodb https://www.npmjs.com/package/mongodb
github項目地址 https://github.com/mongodb/node-mongodb-native
屬於官方提供的mongodb驅動
文檔 http://mongodb.github.io/node-mongodb-native/3.1/api/
項目首頁 http://mongodb.github.io/node-mongodb-native/
屬於一個輪子,已經造好了
安裝mongodb
PS C:\Users\mingm\Desktop\node> npm install mongodb --savenpm WARN saveError ENOENT: no such file or directory, open ‘C:\Users\mingm\Desktop\node\package.json‘npm notice created a lockfile as package-lock.json. You should commit this file.npm WARN enoent ENOENT: no such file or directory, open ‘C:\Users\mingm\Desktop\node\package.json‘npm WARN node No descriptionnpm WARN node No repository field.npm WARN node No README datanpm WARN node No license field.+ [email protected]added 7 packages from 5 contributors and audited 7 packages in 108.492sfound 0 vulnerabilitiesPS C:\Users\mingm\Desktop\node>
建立資料庫
好吧,因為各種版本的緣故,需要使用新的解析器。所以需要設定options的參數
即
useNewUrlParser: true
2.0的驅動程式
舊版本不需要。
PS C:\Users\mingm\Desktop\node> node test.js資料庫連接建立PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } console.log("資料庫連接建立"); db.close();})
串連以後建立了一個mingming的資料庫
> show dbs;admin 0.000GBconfig 0.000GBlocal 0.000GB> use local;switched to db local>
查詢了一下,還沒有,因為沒有往裡面添加資料
建立集合
即,建立一張表
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } console.log("資料庫連接建立"); var dbase = db.db(‘mingming‘); // 選擇資料庫為mingming dbase.createCollection(‘site‘, (err, res) => { // 建立資料庫為mingming下的site集合 if(err) { throw err; } console.log(‘建立集合!‘); db.close(); });});
PS C:\Users\mingm\Desktop\node> node test.js資料庫連接建立建立集合!PS C:\Users\mingm\Desktop\node>
看一下這張表
> show dbs;admin 0.000GBconfig 0.000GBlocal 0.000GBmingming 0.000GB> use mingmingswitched to db mingming> show ceoolection;2018-07-31T23:12:55.014+0800 E QUERY [js] Error: don‘t know how to show [ceoolection] :[email protected]/mongo/shell/utils.js:1043:11[email protected]/mongo/shell/utils.js:755:15@(shellhelp2):1:1> show collections;site> db.site.find();>
CURD插入資料
PS C:\Users\mingm\Desktop\node> node test.js文檔插入成功!PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); var myobj = { name:"mingming", url:"www.iming.info" }; dbo.collection(‘site‘).insertOne(myobj, (err, res) => { // mongodb文法 mingming.site.insert(myObj); if(err) { throw err; } console.log("文檔插入成功!"); db.close(); });});
> db.site.find();{ "_id" : ObjectId("5b607e4d5b8ced1c5cb8a7e4"), "name" : "mingming", "url" : "www.iming.info" }{ "_id" : ObjectId("5b607e6ee7b6e82d604d5a4d"), "name" : "mingming", "url" : "www.iming.info" }>
插入多條資料
> db.site.find().pretty();{ "_id" : ObjectId("5b607e4d5b8ced1c5cb8a7e4"), "name" : "mingming", "url" : "www.iming.info"}{ "_id" : ObjectId("5b607e6ee7b6e82d604d5a4d"), "name" : "mingming", "url" : "www.iming.info"}{ "_id" : ObjectId("5b6080037aa38239e46c0dc1"), "name" : "mingming", "url" : "www.iming.info", "type" : "cn"}{ "_id" : ObjectId("5b6080037aa38239e46c0dc2"), "name" : "google", "url" : "www.google.com.hk", "type" : "un"}{ "_id" : ObjectId("5b6080037aa38239e46c0dc3"), "name" : "twitter", "url" : "www.twitter.com", "type" : "un"}>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); var myobj = [ {name:"mingming", url:"www.iming.info" , type:"cn"}, {name:"google", url:"www.google.com.hk", type:"un"}, {name:"twitter", url:"www.twitter.com", type:"un"}, ]; dbo.collection(‘site‘).insertMany(myobj, (err, res) => { // mongodb文法 mingming.site.insert(myObj); if(err) { throw err; } console.log("文檔插入成功!"); db.close(); });});
PS C:\Users\mingm\Desktop\node> node test.js文檔插入成功!PS C:\Users\mingm\Desktop\node>
查詢資料
PS C:\Users\mingm\Desktop\node> node test.js[ { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘www.iming.info‘ }, { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘www.iming.info‘ }, { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘www.iming.info‘, type: ‘cn‘ }, { _id: 5b6080037aa38239e46c0dc2, name: ‘google‘, url: ‘www.google.com.hk‘, type: ‘un‘ }, { _id: 5b6080037aa38239e46c0dc3, name: ‘twitter‘, url: ‘www.twitter.com‘, type: ‘un‘ } ]PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); dbo.collection(‘site‘).find({}).toArray((err, result) => { // 使用find()查詢完成以後,使用toArray將查詢到的資料完成回調 if (err) { throw err; } console.log(result); // 將回調的結果輸出(由於結果是暫時儲存在記憶體中的,確保有足夠大的記憶體儲存,或者使用流也可) db.close(); });});;
> db.site.find().pretty();{ "_id" : ObjectId("5b607e4d5b8ced1c5cb8a7e4"), "name" : "mingming", "url" : "www.iming.info"}{ "_id" : ObjectId("5b607e6ee7b6e82d604d5a4d"), "name" : "mingming", "url" : "www.iming.info"}{ "_id" : ObjectId("5b6080037aa38239e46c0dc1"), "name" : "mingming", "url" : "www.iming.info", "type" : "cn"}{ "_id" : ObjectId("5b6080037aa38239e46c0dc2"), "name" : "google", "url" : "www.google.com.hk", "type" : "un"}{ "_id" : ObjectId("5b6080037aa38239e46c0dc3"), "name" : "twitter", "url" : "www.twitter.com", "type" : "un"}>
查詢指定條件的資料
PS C:\Users\mingm\Desktop\node> node test.js[ { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘www.iming.info‘ }, { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘www.iming.info‘ }, { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘www.iming.info‘, type: ‘cn‘ } ]PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); var whereStr = {"name":"mingming"}; // 查詢條件 dbo.collection(‘site‘).find(whereStr).toArray((err, result) => { // 使用find()查詢完成以後,使用toArray將查詢到的資料完成回調 if (err) { throw err; } console.log(result); // 將回調的結果輸出(由於結果是暫時儲存在記憶體中的,確保有足夠大的記憶體儲存,或者使用流也可) db.close(); });});;
更新資料
PS C:\Users\mingm\Desktop\node> node test.js文檔更新成功PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); var whereStr = {"name":"mingming"}; // 查詢條件 var updateStr = {$set: {"url": "AmingA"}}; dbo.collection(‘site‘).updateOne(whereStr, updateStr, (err, result) => { // 使用find()查詢完成以後,使用toArray將查詢到的資料完成回調 if (err) { throw err; } console.log(‘文檔更新成功‘); // 將回調的結果輸出(由於結果是暫時儲存在記憶體中的,確保有足夠大的記憶體儲存,或者使用流也可) db.close(); });});;
> db.site.find({"name":"mingming"}).pretty();{ "_id" : ObjectId("5b607e4d5b8ced1c5cb8a7e4"), "name" : "mingming", "url" : "AmingA"}{ "_id" : ObjectId("5b607e6ee7b6e82d604d5a4d"), "name" : "mingming", "url" : "www.iming.info"}{ "_id" : ObjectId("5b6080037aa38239e46c0dc1"), "name" : "mingming", "url" : "www.iming.info", "type" : "cn"}>
mongodb的更新
> db.site.update({"name":"mingming"}, {$set: {"url":""}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.site.find({"name":"mingming"});{ "_id" : ObjectId("5b607e4d5b8ced1c5cb8a7e4"), "name" : "mingming", "url" : "" }{ "_id" : ObjectId("5b607e6ee7b6e82d604d5a4d"), "name" : "mingming", "url" : "www.iming.info" }{ "_id" : ObjectId("5b6080037aa38239e46c0dc1"), "name" : "mingming", "url" : "www.iming.info", "type" : "cn" }>
更新多條資料
PS C:\Users\mingm\Desktop\node> node test.js{ n: 3, nModified: 3, ok: 1 }PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); var whereStr = {"name":"mingming"}; // 查詢條件 var updateStr = {$set: {"url": ""}}; dbo.collection(‘site‘).updateMany(whereStr, updateStr, (err, result) => { // 使用find()查詢完成以後,使用toArray將查詢到的資料完成回調 if (err) { throw err; } console.log(result.result); // 將回調的結果輸出(由於結果是暫時儲存在記憶體中的,確保有足夠大的記憶體儲存,或者使用流也可) db.close(); });});;
> db.site.find({"name":"mingming"});{ "_id" : ObjectId("5b607e4d5b8ced1c5cb8a7e4"), "name" : "mingming", "url" : "" }{ "_id" : ObjectId("5b607e6ee7b6e82d604d5a4d"), "name" : "mingming", "url" : "" }{ "_id" : ObjectId("5b6080037aa38239e46c0dc1"), "name" : "mingming", "url" : "", "type" : "cn" }>
刪除資料
同樣deleteOne和deleteMany()不在闡述
排序
使用sort()完成排序
PS C:\Users\mingm\Desktop\node> node test.js[ { _id: 5b6080037aa38239e46c0dc2, name: ‘google‘, url: ‘www.google.com.hk‘, type: ‘un‘ }, { _id: 5b6080037aa38239e46c0dc3, name: ‘twitter‘, url: ‘www.twitter.com‘, type: ‘un‘ }, { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘‘, type: ‘cn‘ }, { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘‘ }, { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘‘ } ]PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); var mysort = { type:-1 }; // 降序 dbo.collection(‘site‘).find().sort(mysort).toArray((err, result) => { if (err) { throw err; } console.log(result); // 將回調的結果輸出(由於結果是暫時儲存在記憶體中的,確保有足夠大的記憶體儲存,或者使用流也可) db.close(); });});;
mongodb文法
> db.site.find({}).sort({type:-1});{ "_id" : ObjectId("5b6080037aa38239e46c0dc2"), "name" : "google", "url" : "www.google.com.hk", "type" : "un" }{ "_id" : ObjectId("5b6080037aa38239e46c0dc3"), "name" : "twitter", "url" : "www.twitter.com", "type" : "un" }{ "_id" : ObjectId("5b6080037aa38239e46c0dc1"), "name" : "mingming", "url" : "", "type" : "cn" }{ "_id" : ObjectId("5b607e4d5b8ced1c5cb8a7e4"), "name" : "mingming", "url" : "" }{ "_id" : ObjectId("5b607e6ee7b6e82d604d5a4d"), "name" : "mingming", "url" : "" }>
分頁
使用limit,分頁和排序使用的統統是通道(一個來自linux的概念)
PS C:\Users\mingm\Desktop\node> node test.js[ { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘‘ }, { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘‘ } ]PS C:\Users\mingm\Desktop\node>
跳過指定條數
PS C:\Users\mingm\Desktop\node> node test.js[ { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘‘, type: ‘cn‘ }, { _id: 5b6080037aa38239e46c0dc2, name: ‘google‘, url: ‘www.google.com.hk‘, type: ‘un‘ } ]PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); dbo.collection(‘site‘).find().skip(2).limit(2).toArray((err, result) => { if (err) { throw err; } console.log(result); // 將回調的結果輸出(由於結果是暫時儲存在記憶體中的,確保有足夠大的記憶體儲存,或者使用流也可) db.close(); });});;
隨機讀取一條資料
PS C:\Users\mingm\Desktop\node> node test.js[ { _id: 5b6080037aa38239e46c0dc2, name: ‘google‘, url: ‘www.google.com.hk‘, type: ‘un‘ } ][ { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘‘, type: ‘cn‘ } ][ { _id: 5b6080037aa38239e46c0dc2, name: ‘google‘, url: ‘www.google.com.hk‘, type: ‘un‘ } ][ { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘‘, type: ‘cn‘ } ][ { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘‘ } ]PS C:\Users\mingm\Desktop\node> node test.js[ { _id: 5b6080037aa38239e46c0dc2, name: ‘google‘, url: ‘www.google.com.hk‘, type: ‘un‘ } ][ { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘‘, type: ‘cn‘ } ][ { _id: 5b6080037aa38239e46c0dc3, name: ‘twitter‘, url: ‘www.twitter.com‘, type: ‘un‘ } ][ { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b607e6ee7b6e82d604d5a4d, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘‘, type: ‘cn‘ } ][ { _id: 5b6080037aa38239e46c0dc1, name: ‘mingming‘, url: ‘‘, type: ‘cn‘ } ][ { _id: 5b607e4d5b8ced1c5cb8a7e4, name: ‘mingming‘, url: ‘‘ } ][ { _id: 5b6080037aa38239e46c0dc3, name: ‘twitter‘, url: ‘www.twitter.com‘, type: ‘un‘ } ][ { _id: 5b6080037aa38239e46c0dc3, name: ‘twitter‘, url: ‘www.twitter.com‘, type: ‘un‘ } ]PS C:\Users\mingm\Desktop\node>
var MongoClient = require(‘mongodb‘).MongoClient;var url = "mongodb://127.0.0.1:27017/mingming"; // 串連的urlMongoClient.connect(url, { useNewUrlParser: true }, (err, db) => { // 調用封裝好的connect按照url建立和MongoDB之間的串連 if (err) { throw err; } var dbo = db.db(‘mingming‘); for(var i=0; i<10; i++){ dbo.collection(‘site‘).aggregate([{$sample: {size:1}}]).toArray((err, result) => { if (err) { throw err; } console.log(result); // 將回調的結果輸出(由於結果是暫時儲存在記憶體中的,確保有足夠大的記憶體儲存,或者使用流也可) }); }; db.close();});;
使用的同樣是管道
部落格
www.iming.info
node 串連mongodb