如何使用各种参数在猫鼬中进行搜索查询?
Posted
技术标签:
【中文标题】如何使用各种参数在猫鼬中进行搜索查询?【英文标题】:how to do a search query in mongoose with various parameters? 【发布时间】:2017-08-16 21:07:21 【问题描述】:我现在创建了一个 Jobs API,我想创建一个包含技能和位置的搜索功能。例如,如果我使用技能 html、CSS、javascript 和特定位置进行搜索,那么它应该显示与查询匹配的工作列表。
JSON 数据:
"jobID": 1245,
"title": "Front-End Developer",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
"Skills": "JavaScript, jQuery, Angular.js, React.js, Ember.js",
,
"jobID": 2982,
"title": "Back-End Developer",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
"Skills": "JavaScript, Node.js, Express.js, MongoDB"
,
"jobID": 5782,
"title": "Full-Stack Developer",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
"Skills": "JavaScript, jQuery, Angular.js, backbone.js"
,
"jobID": 4674,
"title": "Java Developer",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
"Skills": "Java, J2EE, JSON, JBOSS, Spring, Hibernate"
,
"jobID": 1589,
"title": "Hadoop Developer",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."
"Skills": Hadoop, Spark, Teradata, Scala, Oozie, Java, etc."
“工作ID”:1245, "title": "前端开发人员", "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus。" "技能": "JavaScript, jQuery, Angular.js, React.js, Ember.js", “地点”:印度 , “工作ID”:2982, "title": "后端开发人员", "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus。" "技能": "JavaScript, Node.js, Express.js, MongoDB", “地点”:美国 , “工作ID”:5782, "title": "全栈开发者", "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus。" "技能": "JavaScript、jQuery、Angular.js、backbone.js", “地点”:英国 , “工作ID”:4674, "title": "Java 开发者", "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus。" “技能”:“Java、J2EE、JSON、JBOSS、Spring、Hibernate”, “地点”:俄罗斯 , “工作ID”:1589, "title": "Hadoop 开发者", "description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus。" “技能”:Hadoop、Spark、Teradata、Scala、Oozie、Java 等“, “地点”:中国
Route.js 文件
'use strict';
var express = require('express'),
router = express.Router(),
search = require('../controllers/search');
Router.get('/:query', search.get);
module.exports = router;
Search.js 文件
'use strict';
var mongoose = require('mongoose'),
Jobs = mongoose.model('Jobs');
var search =
get: function (req, res)
var searchQuery = req.params.query;
Jobs.findOne(search : searchQuery, function(err, result)
if (err) throw err;
console.log(result);
res.json(result);
);
;
module.exports = search;
Server.js 文件
'use strict';
var express = require('express'),
app = express();
//Routes
var jobRoute = require('./routes/router');
app.use('/api/jobs', jobRoute);
//Search Routes
var searchRoute = require('./routes/search-route');
app.use('/api/search', searchRoute);
//Server Connection
app.listen(port, function()
console.log('Listening on port 8080!');
【问题讨论】:
【参考方案1】:你可以试试这个
Jobs.find(skill: "html",location:"US", function(err, user)
if (err)
res.send(err);
);
【讨论】:
路由器参数我应该如何通过? 抱歉,请务必回答您的问题。 我的 Route.js 和 search.js 文件的代码是否正确?这是正确的写法吗? GET /search?q=skill=html&location=US.. 类似这样的 我觉得没问题!【参考方案2】:多参数搜索,可以用逗号分隔
Jobs.find(location:"wherever",skills:$in:["Java","Javascript"], function(err, data)
//your code
$in
参数接受一个数组作为参数,并将返回包含数组中某个字段的所有文档。
【讨论】:
如果我以这种方式通过,我会在数据中得到一个空数组 [ ]。 您的收藏在数据库中的名称是什么?请区分大小写 我发现问题是因为区分大小写! 我明白了,你介意至少支持我的回答吗? 不,我可以投票赞成你的,因为声誉较低。要进行投票,我们必须至少拥有 15 个声望。以上是关于如何使用各种参数在猫鼬中进行搜索查询?的主要内容,如果未能解决你的问题,请参考以下文章