MongoDB :如何重新格式化此代码以在名称字段中包含排序?
MongoDB : How can I reformat this code to include a sort on the name field?
如何重新格式化此块以便在名称字段上添加排序?
Router.route('/hospitals/search/:searchString')
.get(function (req, res) {
const searchString = decodeURIComponent(req.params.searchString)
HospitalModel.find({
$or: [
{Name: {$regex: searchString, $options: 'i'}},
{City: {$regex: searchString, $options: 'i'}}
]
}, function (err, data) {
const response = (err)
? {error: true, message: 'Server Error when querying hospitals collection.'}
: {error: false, message: data}
res.json(response)
})
})
总的来说,我对 mongo 和 API 创作都不熟悉。我发现的例子并不完全符合我写这篇文章的方式,此时我有点不知所措。
试试这个代码。排序可以在 mongoose 中以不同的方式完成。
以下是相关示例
HospitalModel.find({}).sort('Name').exec(function(err, docs) { ... });
HospitalModel.find({}).sort({Name: 1}).exec(function(err, docs) { ... });
HospitalModel.find({}, null, {sort: {Name: 1}}, function(err, docs) { ... });
HospitalModel.find({}, null, {sort: [['Name', -1]]}, function(err, docs) { ... });
一般我遵循的都是这样。它对你也有用。
Router.route('/hospitals/search/:searchString')
.get(function(req, res) {
const searchString = decodeURIComponent(req.params.searchString)
HospitalModel.find({
$or: [
{ Name: { $regex: searchString, $options: 'i' } },
{ City: { $regex: searchString, $options: 'i' } }
]
}, null, { sort: { Name: 1 } }, function(err, data) {
const response = (err) ? { error: true, message: 'Server Error when querying hospitals collection.' } : { error: false, message: data }
res.json(response)
})
})
试试下面的代码
HospitalModel.find(
$or: [{
Name: {
$regex: searchString,`
$options: 'i'
}
}, {
City: {
$regex: searchString,
$options: 'i'
}
}]
) .sort({
Name: 1
}).exec(function(err, data) {
const response = (err) ? {
error: true,
message: 'Server Error when querying hospitals collection.'
} : {
error: false,
message: data
}
return res.json(response);
});
如何重新格式化此块以便在名称字段上添加排序?
Router.route('/hospitals/search/:searchString')
.get(function (req, res) {
const searchString = decodeURIComponent(req.params.searchString)
HospitalModel.find({
$or: [
{Name: {$regex: searchString, $options: 'i'}},
{City: {$regex: searchString, $options: 'i'}}
]
}, function (err, data) {
const response = (err)
? {error: true, message: 'Server Error when querying hospitals collection.'}
: {error: false, message: data}
res.json(response)
})
})
总的来说,我对 mongo 和 API 创作都不熟悉。我发现的例子并不完全符合我写这篇文章的方式,此时我有点不知所措。
试试这个代码。排序可以在 mongoose 中以不同的方式完成。
以下是相关示例
HospitalModel.find({}).sort('Name').exec(function(err, docs) { ... });
HospitalModel.find({}).sort({Name: 1}).exec(function(err, docs) { ... });
HospitalModel.find({}, null, {sort: {Name: 1}}, function(err, docs) { ... });
HospitalModel.find({}, null, {sort: [['Name', -1]]}, function(err, docs) { ... });
一般我遵循的都是这样。它对你也有用。
Router.route('/hospitals/search/:searchString')
.get(function(req, res) {
const searchString = decodeURIComponent(req.params.searchString)
HospitalModel.find({
$or: [
{ Name: { $regex: searchString, $options: 'i' } },
{ City: { $regex: searchString, $options: 'i' } }
]
}, null, { sort: { Name: 1 } }, function(err, data) {
const response = (err) ? { error: true, message: 'Server Error when querying hospitals collection.' } : { error: false, message: data }
res.json(response)
})
})
试试下面的代码
HospitalModel.find(
$or: [{
Name: {
$regex: searchString,`
$options: 'i'
}
}, {
City: {
$regex: searchString,
$options: 'i'
}
}]
) .sort({
Name: 1
}).exec(function(err, data) {
const response = (err) ? {
error: true,
message: 'Server Error when querying hospitals collection.'
} : {
error: false,
message: data
}
return res.json(response);
});