1
| > npm install --save mongodb
|
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
| //mongodb 3以上寫法 const MongoClient = require('mongodb').MongoClient;
//TodoApp是一個(gè)數(shù)據(jù)庫名字,mongo中不需要?jiǎng)?chuàng)建數(shù)據(jù)庫 MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{ if(err){ return console.log('Unable to connect to MongoDB server'); } console.log('Connect to MongoDB server'); const db = client.db('TodoApp');
//插入數(shù)據(jù) db.collection('Todos').insertOne({ text:'Something to do', completed:false },(err,result)=>{ if(err){ return console.log('unable to insert todo',err); } console.log(JSON.stringify(result.ops,undefined,2)); }); //關(guān)閉客戶端 client.close();
});
|
id
插入一個(gè)對象的時(shí)候,會自動的產(chǎn)生一個(gè)隨機(jī)的id。id包含了時(shí)間戳和機(jī)器識別碼。
我們可以借助與mongo的方法,為我們產(chǎn)生一個(gè)隨機(jī)數(shù)。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),方城企業(yè)網(wǎng)站建設(shè),方城品牌網(wǎng)站建設(shè),網(wǎng)站定制,方城網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,
方城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
1 2 3 4 5
| const {MongoClient,ObjectID} = require('mongodb'); var obj = new ObjectID(); console.log(obj); //返回時(shí)間 console.log(obj.getTimestamp());
|
參考資料:
MongoDB Node.js Driver Documentation
node-mongodb-native
查詢
原始數(shù)據(jù):
1 2 3 4 5 6 7 8 9 10 11 12
| [ { "_id": "5bee9e3794c5f22eb989f6eb", "text": "Something to do", "completed": false }, { "_id": "5beeaf0a2d48eb2b7c90758f", "text": "Something to do", "completed": true } ]
|
js代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| const {MongoClient,ObjectID} = require('mongodb');
MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{ if(err){ return console.log('Unable to connect to MongoDB server'); } console.log('Connect to MongoDB server'); const db = client.db('TodoApp');
//返回所有document db.collection('Todos').find().toArray().then((docs)=>{ console.log('Todos'); console.log(JSON.stringify(docs,undefined,2)); },(err)=>{ console.log('unable to fetch todos', err); })
client.close(); });
|
執(zhí)行代碼返回:
1 2 3 4 5 6 7 8 9 10 11 12
| [ { "_id": "5bee9e3794c5f22eb989f6eb", "text": "Something to do", "completed": false }, { "_id": "5beeaf0a2d48eb2b7c90758f", "text": "Something to do", "completed": true } ]
|
篩選
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| const {MongoClient,ObjectID} = require('mongodb');
MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{ if(err){ return console.log('Unable to connect to MongoDB server'); } console.log('Connect to MongoDB server'); const db = client.db('TodoApp'); db.collection('Todos').find({completed:true}).toArray().then((docs)=>{ console.log('Todos'); console.log(JSON.stringify(docs,undefined,2)); },(err)=>{ console.log('unable to fetch todos', err); })
client.close();
});
|
執(zhí)行代碼返回:
1 2 3 4 5 6 7
| [ { "_id": "5beeaf0a2d48eb2b7c90758f", "text": "Something to do", "completed": true } ]
|
篩選id
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| const {MongoClient,ObjectID} = require('mongodb');
MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{ if(err){ return console.log('Unable to connect to MongoDB server'); } console.log('Connect to MongoDB server'); const db = client.db('TodoApp'); db.collection('Todos').find({ new ObjectID('5beeaf0a2d48eb2b7c90758f') }).toArray().then((docs)=>{ console.log('Todos'); console.log(JSON.stringify(docs,undefined,2)); },(err)=>{ console.log('unable to fetch todos', err); })
client.close();
});
|
執(zhí)行代碼返回:
1 2 3 4 5 6 7
| [ { "_id": "5beeaf0a2d48eb2b7c90758f", "text": "Something to do", "completed": true } ]
|
刪除document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| const {MongoClient,ObjectID} = require('mongodb');
MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{ if(err){ return console.log('Unable to connect to MongoDB server'); } console.log('Connect to MongoDB server'); const db = client.db('TodoApp'); // db.collection('Todos').deleteMany({text:'mike'}); 刪除全部
//刪除一個(gè) // db.collection('Todos').deleteOne({text:'mike'}).then((result)=>{ // console.log(result); // });
//刪除并返回對象 db.collection('Todos').findOneAndDelete({text:'mike'}).then((result)=>{ console.log(result); });
client.close();
});
|
更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const {MongoClient,ObjectID} = require('mongodb');
MongoClient.connect('mongodb://localhost:27017/TodoApp',(err,client)=>{ if(err){ return console.log('Unable to connect to MongoDB server'); } console.log('Connect to MongoDB server'); const db = client.db('TodoApp');
//查找并更新 db.collection('Todos').findOneAndUpdate({text:'mike'},{ $set:{ completed:false //設(shè)置更改 } },{ returnOriginal:false //返回的結(jié)果為更新后的值。 }).then((result)=>{ console.log(result); //打印跟新結(jié)果 });
client.close(); });
|
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
當(dāng)前文章:nodejs漸入佳境[18]-mongodb-node增刪查改-創(chuàng)新互聯(lián)
本文URL:
http://weahome.cn/article/cejshe.html