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是一个数据库名字,mongo中不需要创建数据库 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').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)); }); //关闭客户端 client.close();
});
|
id
插入一个对象的时候,会自动的产生一个随机的id。id包含了时间戳和机器识别码。
我们可以借助与mongo的方法,为我们产生一个随机数。
创新互联是一家集网站建设,方城企业网站建设,方城品牌网站建设,网站定制,方城网站建设报价,网络营销,网络优化,
方城网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
1 2 3 4 5
| const {MongoClient,ObjectID} = require('mongodb'); var obj = new ObjectID(); console.log(obj); //返回时间 console.log(obj.getTimestamp());
|
参考资料:
MongoDB Node.js Driver Documentation
node-mongodb-native
查询
原始数据:
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(); });
|
执行代码返回:
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();
});
|
执行代码返回:
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();
});
|
执行代码返回:
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'}); 删除全部
//删除一个 // 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 //设置更改 } },{ returnOriginal:false //返回的结果为更新后的值。 }).then((result)=>{ console.log(result); //打印跟新结果 });
client.close(); });
|
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文名称:nodejs渐入佳境[18]-mongodb-node增删查改-创新互联
标题链接:
http://cdkjz.cn/article/cejshe.html