Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB:如何重新格式化此代码以在名称字段中包含排序?_Javascript_Node.js_Mongodb_Api Design - Fatal编程技术网

Javascript MongoDB:如何重新格式化此代码以在名称字段中包含排序?

Javascript MongoDB:如何重新格式化此代码以在名称字段中包含排序?,javascript,node.js,mongodb,api-design,Javascript,Node.js,Mongodb,Api Design,如何重新格式化此块,以便在名称字段上添加排序 Router.route('/hospitals/search/:searchString') .get(function (req, res) { const searchString = decodeURIComponent(req.params.searchString) HospitalModel.find({ $or: [ {Name: {$reg

如何重新格式化此块,以便在名称字段上添加排序

   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的创建都是新手。我发现的示例与我写这篇文章的方式不太相符,我现在有点不知所措。

试试这段代码。在猫鼬中可以用不同的方式进行分类

下面是一些例子

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({}).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);
     });
试试下面的代码

       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);
     });

非常感谢你。工作起来很有魅力。我很欣赏这些例子,非常感谢。工作起来很有魅力。我很欣赏这些例子。