日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

node-mongo封装

發(fā)布時(shí)間:2025/6/17 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 node-mongo封装 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
node 里面調(diào)用mongo封裝了下。mongo.js文件 const { MongoClient, ObjectId } = require('mongodb'); const mongourl = "mongodb://localhost:27017/";const findMongo = (dbname, collection, where, req, res) => {MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {if (err) throw err;const db = client.db(dbname);db.collection(collection).find(where).sort({ uptime: -1 }).toArray(function (err, datas) {if (err) throw err;res.writeHead(200, { "Content-Type": "text/plain; charset=utf8" });res.end(JSON.stringify(datas));//client.close();});});return; }const insertMongo = async (dbname, collection, newdatas, req, res) => {MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, client) {if (err) throw err;const db = client.db(dbname);db.collection(collection).insertMany(newdatas, function (err, datas) {if (err) throw err;res.writeHead(200, { "Content-Type": "text/plain; charset=utf8" });res.end(JSON.stringify(datas));//client.close();});});return; }module.exports = {findMongo,insertMongo };測(cè)試調(diào)用文件 const mongo = require('./mongo'); const { ObjectId } = require('mongodb'); const _ = require('lodash');const main = async () => {// const datas = mongo.findMongo("data", "wck", {"_id" : ObjectId("59e05df004fe65001b841ec7")});// console.log(datas);// const datas = mongo.updateMongo("data", "wck", {"_id" : ObjectId("59e05df004fe65001b841ec7")}, {contact:'cc'});// console.log(datas);// const datas = mongo.insertMongo("data", "wck", [{x:'x'},{y:'y'},{z:'z'}]);// console.log(datas);// const datas = mongo.deleteMongo("data", "wck", { "x": "x", "_id": ObjectId("5d91d0d5c5701e074d4174ed") });// console.log(datas); }main();可以在上面的基礎(chǔ)上繼續(xù)封裝,然后把mongo數(shù)據(jù)數(shù)據(jù)庫(kù)單獨(dú)掛在一個(gè)服務(wù)器上或者是docker鏡像上,通過(guò)url get put post 等方式訪問(wèn)更新操作。目前我正在封裝這個(gè),接口類(lèi)似: http://127.0.0.1:2323/?k=q&n=data&c=wck&w={"y":"y"} 注意安全問(wèn)題,比如鎖死ip。

?

總結(jié)

以上是生活随笔為你收集整理的node-mongo封装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。