如何根据输入字段/文本从mongoDB查询/获取数据

时间:2019-01-20 04:18:44

标签: node.js angular mongodb express mongodb-query

在那里。感谢您点击这篇文章。我想知道如何根据输入字段从mongodb获取/查询数据。例如,如果我要在名为“ location”的输入字段中输入“ USA”,它将仅获取其中包含USA的数据,并且该数据将显示在下一页上。 当我单击一个按钮时,如何使它工作。 我将链接一些网站和代码的屏幕截图(我觉得我的代码是完全错误的。)

error im facing after pasting ur code and code for passing data

this is my home.component.html line40. it is to pass the data to next page

home page where i will be inserting a country in the enter country field, once i click on "find" it will GET data from mongo based on the country ive insertted.

this is the page where the data will be displayed

这是我尝试弄乱的一些代码(经过测试是否有效)

 //this is in my api.js file
//get data from "listing"(which is my db collection) based on "inputcountry( the inputfield)"
router.get('/home', function(req, res) {
    db.collection('listing').find({"inputCountry": req.body.inputCountry}).toArray( (err, results) => {
        console.log('got search')
        res.send(results)});
    });


//this is in my service .ts file (named getservice)
//this is for the searching of house based on location
  searchHouse() {
    return this.http.get<any[]>('./api/home');
    }
    
    //this is in my home. component.ts file (where the searching will happen)
     constructor(private fb: FormBuilder, private router: Router, private getService: GetService) {
  // Retrieve posts from the API
    this.getService.searchHouse().subscribe(searchedhouses => {
    this.searchedhouses = searchedhouses;
  });
  }
  
<!--this is in my home.component.html where there is the input field named "inputcountry" and submit button -->

 <form [formGroup] = "myForm" (ngSubmit) = "searchHouse (myForm)">
            <div class="form-group">
            <label for="inputCountry">Enter Country</label>
            <input type="text" class="form-control"  formControlName="inputCountry" id="inputCountry"   name = "inputCountry" placeholder="Location e.g Singapore              .">
            </div>
            
              <button class="btn btn-lg btn-primary btn-block text-uppercase" routerLink="/houses" type="submit" >Find</button>
     
            </form>
            

2 个答案:

答案 0 :(得分:2)

您需要使用regexp才能根据输入字段在mongodb中搜索数据。

https://docs.mongodb.com/manual/reference/operator/query/regex/

答案 1 :(得分:0)

好吧,按照代码

在后端代码中,对于api,您可以放置​​一个Regexified输入, 对于预期的输出,

router.get('/home', function(req, res) {
    db.collection('listing').find({"inputCountry": { $regex: req.query.inputCountry ,$options: 'i' }}).toArray( (err, results) => {
        console.log('got search')
        res.send(results)});
    });

上面的代码将为您提供输出结果。您可以在下一页中显示的内容可能位于前端。

在角度上,有很多更好的方法可供使用,总之,您可以这样做:

 onSubmit = function (allValues) {
    console.log(allValues);
    this.http.get("http://localhost/home?inputCountry="+allValues.inputCountry).subscribe((data) => {
   //you will get the result here, show that to user. 
});
  };


<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
  <div class="form-group">
    <label for="inputCountry">Enter Country</label>
    <input
      type="text"
      class="form-control"
      formControlName="inputCountry"
      id="inputCountry"
      name="inputCountry"
      placeholder="Location e.g Singapore              ."
    />
  </div>

  <button
    class="btn btn-lg btn-primary btn-block text-uppercase"
    routerLink="/houses"
    type="submit"
  >
    Find
  </button>
</form>