//在index.html中定义
<script>
var host = "http://localhost:8888"
</script>
add-插入一条或多条数据
query-分页、条件、排序一体查询
update-更新数据
delete-删除数据
sort-排序
deleteCol-删除集合,谨慎使用!建议后端关闭该接口
本地安装node http://nodejs.cn/download/
本地安装mongodb https://www.mongodb.com/download-center/community
cd node
//安装依赖
npm install
//启动数据库链接
node app.js
//定义数据,第一次如果没有表和数据库则自动创建
var datas = {
"dataBase" : "SFAPI",
"collectionName" : "userInfo",
"data":[
{
"userInfo" : {
"name": "sf",
"email" : "[email protected]"
},
"stars": "4"
},
{
"userInfo" : {
"name": "what",
"email" : "[email protected]"
},
"stars": "1000"
}
]
}
$.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);
})
//不需要分页、条件查询、排序可以不写对应字段
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)
})
//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)
})
//whereStr查询条件
var datas = {
"dataBase" : "SFAPI",
"collectionName" : "userInfo",
"whereStr" : {
"stars": "4"
}
}
axios.post(host + "/delete",datas)
.then(res => {
console.log(res.data)
})
//按指定字段排序,升序(1)降序(-1)
var datas = {
"dataBase" : "SFAPI",
"collectionName" : "userInfo",
"sort" : {
"createTime": -1
}
}
axios.post(host + "/sort",datas)
.then(res => {
console.log(res.data)
})
var datas = {
"dataBase" : "SFAPI",
"collectionName" : "userInfo"
}
axios.post(host + "/deleteCol",datas)
.then(res => {
console.log(res.data)
})