node连接mongodb查找数据
model/user.js
const mongodb = require('mongoose')
const Schema = mongodb.Schema;
const UserModel = new Schema({
name: String,
id: String,
tel: String,
hobby: String,
updateDate: String,
})
const Model = mongodb.model('UserModal', UserModel, 'user');
module.exports = Model
service\user.js
const get = async (params) => {
const data = await Model.find();
return data
}
find()
查询集合中所有的内容并批量返回
const get = async (params) => {
const data = await Model.find({}, {
"_id": 0,
"__v": 0
});
return data
}
find({},{})
, 第二个参数键值设置0是剔除不需要返回的键,第二个参数如果设置1是指定需要返回的。
{
"_id": 0,
"__v": 0
}
const get = async (params) => {
const data = await Model.find({}, {
"name": 1,
id: 1,
"hobby": 1
});
return data
}
example
index.js
const {
server
} = require('./server/index')
const {
connectDB
} = require("./db/index.js")
const User = require("./controller/user")
const Url = require('node:url');
// 连接mongodb数据库
connectDB().then(res => {})
// 监听请求
server.on('request', async function (req, res) {
const {
method,
url,
} = req
const {
pathname,
} = Url.parse(url)
if (method === 'GET') {
if (pathname === '/user') {
User.get(req, res)
}
} else if (method === 'POST') {
if (pathname === '/user') {
User.bitchAdd(req, res)
}
} else if (method === 'DELETE') {
} else if (method === 'PUT') {
}
})
server.listen(3000, "localhost", () => {
// console.log('listen');
})
service\user.js
const Model = require("../model/user")
const get = async (params) => {
const data = await Model.find(params, {
"name": 1,
id: 1,
"hobby": 1,
"_id": 0,
});
return data
}
const add = async (params) => {
// 判断数据是否已存在,存在不可添加
const flag = Model.find()
const data = await Model.insertMany(params);
console.log('data', data);
let res = {}
if (data.length) {
res = {
code: 0,
data: null,
msg: 'success'
}
} else {
res = {
code: 1001,
data: null,
msg: 'fail'
}
}
return res
}
const del = async (params) => {
// 找到需要删除的数据_id
let idList = [];
// 查找集合中的数据(user)
const data = await Model.find({
name: '木子'
});
let res;
if (data.length) {
idList = data.map(v => v._id)
res = await Model.deleteMany({
_id: {
$in: idList || []
}
});
}
if (res.deletedCount > 0) {
}
}
const update = async (params) => {
const data = await Model.find();
return data
}
module.exports = {
get,
add,
del,
update
}
controller\user.js
const querystring = require('node:querystring');
const url = require('node:url');
const Service = require("../service/user")
const get = async (request, response) => {
const {
pathname,
query
} = url.parse(request.url, true)
console.log('query', query);
const res = await Service.get(query);
console.log();
response.end(JSON.stringify({
code: 0,
data: {
lis: res,
query: query
}
}))
}
const add = () => {
}
const bitchAdd = (request, response) => {
let params;
request.on('data', function (chunk) {
params = chunk
console.log('chunk', params);
})
request.on('end', async function () {
console.log('params', params.toString());
console.log('处理后params', );
const item = querystring.parse(params.toString())
const res = await Service.add(item)
response.end(JSON.stringify(res))
})
}
const del = () => {
}
const bitchDelete = () => {
}
module.exports = {
get,
add,
bitchAdd,
del,
bitchDelete
}