【问题标题】:How do i query/get data from mongoDB based on input field/text如何根据输入字段/文本从 mongoDB 查询/获取数据
【发布时间】:2019-01-20 04:18:44
【问题描述】:

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

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>
            

【问题讨论】:

  • 在更改该输入字段时,使用该输入字段值调用 api。现在在那个api中,在mongo中找到Regex可能是。并返回值以显示它,
  • @saikatchakrabortty 呃我真的不明白你刚才说什么。你介意把它写成答案之一吗?
  • 确认我是否这是您想要的,然后将作为答案:代替{"inputCountry": req.body.inputCountry} do: {"inputCountry": { $regex: req.body.inputCountry ,$options: 'i' }}.find 的查询部分。
  • @saikatchakrabortty $option : 'i' 做什么?我必须安装任何东西才能使正则表达式工作吗?
  • @saikatchakrabortty 是否可以做到,以便当我单击查找按钮时它会获取数据?并在下一页显示?

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


【解决方案1】:

您需要使用正则表达式才能根据您的输入字段在 mongodb 中搜索数据。

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

【讨论】:

    【解决方案2】:

    好吧,按照代码,

    在后端代码中,对于 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)});
        });
    
    

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

    在 Angular 中,有很多更好的方法可供简而言之,您可以执行以下操作:

     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>
    
    
    

    【讨论】:

    • 但我如何在另一个 page.component.html 中而不是在 url 中显示它们?
    • 我可以使用路由来传递我检索到的数据吗?从一个组件到另一个组件。那么对于我的情况,将数据从主组件传递到下一个组件?
    • 我想这可以帮助你,如何将数据传递给其他组件:stackoverflow.com/questions/36835123/…
    • 顺便说一句你的意思是什么--->> //你会在这里得到结果,显示给用户。
    • 这只是一个注释,您将在其中获取数据,并且您可以使用该数据来显示,可能您只能在那里显示,或者您也可以将其发送到其他地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 2014-12-04
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多