- mongodb安装
命令行到这个目录下 /Users/admin/MongoDB/mongodb-osx-x86_64-3.4.1/bin
这一步是启动mongodb server ,启动成功后最后一行应该是端口号
下一步
http://127.0.0.1:27017/打开也会看到提示
- nodejs mongoose
sudo npm install mongoose -save
- $in 多个参数查询
- lean() mongoose返回的并不是一个Object,是它自定义的一个对象,加上lean()。
lean()的官网解释:
Documents returned from queries with the
lean
option enabled are plain javascript objects, not
MongooseDocuments
.
- vue-resource $http.get:
searchnodesSearch(){
//两个搜索输入框都为空时
if(!this.nodegroupnameSearch&&!this.typeSearch){
this.getNodes()
}else {
this.$http.get('/api/node/search’,{
params:[this.nodegroupnameSearch,this.typeSearch]}).then(response => {
console.log(response)
if (response.body) {
this.nodes = response.body;
}
})
}
}
在后台用req.query来获取
- mongoose模糊查询,通过传入query对象,就可以进行查询。对应mongodb中,可以直接使用 ‘/../’ 斜杠。但是在nodejs中,必须要使用RegExp,来构建正则表达式对象。
router.get(API+
'/node/search',(req,res)=>{
if(req.query[0]&&req.query[1]) {
let query={}
query[
'nodegroupnames']=
new RegExp(req.query[0])
query[
'type']=
new RegExp(req.query[1])
models.
Nodes.find(query).lean().exec((err, data) => {
...
})
}
...
})