Skip to content

前端通过请求使用的mongoDB数据库增删改查api

License

Notifications You must be signed in to change notification settings

SSSSSFFFFF/SF-mongodb-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

前后端分离,ajax+node+mongodb ,提供前端使用的数据库增删改查api。

运行后端app.js后使用,mongodb的ip密码配置在根目录下config.json

接口文档

//在index.html中定义
<script>
    var host = "http://localhost:8888"
</script>

add-插入一条或多条数据
query-分页、条件、排序一体查询
update-更新数据
delete-删除数据
sort-排序
deleteCol-删除集合,谨慎使用!建议后端关闭该接口


后端:node链接Mongodb,提供可供前端使用的操作数据库接口

本地安装node http://nodejs.cn/download/
本地安装mongodb https://www.mongodb.com/download-center/community

cd node 

//安装依赖
npm install

//启动数据库链接
node app.js

示例

Image text Image text


接口文档

add-插入一条或多条数据

//定义数据,第一次如果没有表和数据库则自动创建
var datas = {
	"dataBase" : "SFAPI",
	"collectionName" : "userInfo",
	"data":[
		{
            "userInfo" : {
                "name": "sf",
                "email" : "[email protected]"
            },
            "stars": "4"
		},
		{
            "userInfo" : {
                "name": "what",
                "email" : "[email protected]"
            },
            "stars": "1000"
		}
	]
}
发送jq-ajax和axios写法,后面接口只提供axios
$.ajax({
    type: "post",
    url: host + "/add",
    data: JSON.stringify(datas),
    contentType : 'application/json',
    success: function (res) {
        console.log(res)
    },
    error:function(err){
        console.log(err)
    }
});
axios.post(host + "/add",datas)
    .then(res => {
        console.log(res.data)
    })
    .catch(err => {
        console.error(err); 
 })

query-分页、条件、排序一体查询

//不需要分页、条件查询、排序可以不写对应字段
var datas = {
	"dataBase" : "SFAPI",
	"collectionName" : "userInfo",
	"page": 1,      //第几页
	"pageSize" : 2, //每页显示个数
	"data": {       //查询条件
		"userInfo.name":"sf"
	},
	"sort":{        //按字段排序,升序(1)降序(-1)
		"createTime": -1
	}
}
axios.post(host + "/query",datas)
    .then(res => {
        console.log(res.data)
    })

update-更新数据

//whereStr查询条件
//updateStr更改的内容
var datas = {
	"dataBase" : "SFAPI",
	"collectionName" : "userInfo",
	"whereStr" : {
		"stars": "4"
	},
	"updateStr" : {
		"createTime": "timeChanged"
	}
}
axios.post(host + "/update",datas)
    .then(res => {
        console.log(res.data)
    })

delete-删除数据

//whereStr查询条件
var datas = {
	"dataBase" : "SFAPI",
	"collectionName" : "userInfo",
	"whereStr" : {
		"stars": "4"
	}
}
axios.post(host + "/delete",datas)
    .then(res => {
        console.log(res.data)
    })

sort-排序

//按指定字段排序,升序(1)降序(-1)
var datas = {
	"dataBase" : "SFAPI",
	"collectionName" : "userInfo",
	"sort" : {
		"createTime": -1
	}
}
axios.post(host + "/sort",datas)
    .then(res => {
        console.log(res.data)
    })

deleteCol-删除集合,谨慎使用!建议后端关闭该接口

var datas = {
	"dataBase" : "SFAPI",
	"collectionName" : "userInfo"
}
axios.post(host + "/deleteCol",datas)
    .then(res => {
        console.log(res.data)
    })

About

前端通过请求使用的mongoDB数据库增删改查api

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published