【问题标题】:Multiple search and filter Meteor & Mongodb多重搜索和过滤 Meteor & Mongodb
【发布时间】:2016-10-20 04:27:49
【问题描述】:

我有简单的收藏:

nom: {
    type: String,

},
prenom:{
    type: String,

},

categorie:{
  type: String,

},
age:{
    type:String,

    } ,

ville : {

    type : String 

    }

我想找到一种简单的方法来将多个搜索和过滤元素放入一个表中。例如按姓名和年龄过滤或按年龄和城市过滤。或全部过滤 这是我的桌子: my table

在我的情况下,在没有搜索引擎包的情况下,在 mongodb 和流星上制作这个多重过滤器的最佳方法是什么

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    这就是我倾向于进行搜索的方式,因此它也能找到类似的结果。因此,如果我的结果中有一个字母“A”,如果我在“A”上搜索,它会显示带有字母“A”的所有内容,直到我更具体为止。

    请注意,您可能希望将搜索值和搜索键设为小写,因为 "A" != "a" 并且不会返回任何内容。

    我还设置了一个 int 值,以防出现问题。

    let collection = yourCollection.find({
           "nom": {
                $regex: ".*CONDITION_VALUE.*"
           },
           "prenom": {
                $regex: ".*CONDITION_VALUE.*"
            },
            "categorie": {
                $regex: ".*CONDITION_VALUE.*"
            },
            "age": {
                $gt: minAge,
                $lt: maxAge
            },
            "ville": {
                $regex: ".*CONDITION_VALUE.*"
            }
     });
    

    【讨论】:

      【解决方案2】:

      MongoDB 上使用直接查询时,您可以使用以下示例:

      检查字段是否存在的最简单方法是使用$exist

      db.collectionName.find({fieldName: {$exists: true}})
      

      如果您正在寻找特定的字段值:

      db.collectionName.find({'fieldName': 'value'})
      

      $or 和存在的组合可能如下所示:

      db.collectionName.find({ $or: [ {name: {$exists: true}}, {age: {$exists: true}} ] })
      

      对于多个$or,您可以使用以下查询:

      db.collectionName.find({ $or: [ {name: {$exists: true}}, {age: {$exists: true}} ] },
                             { $or: [ {age: {$exists: true}}, {city: {$exists: true}} ] })
      

      【讨论】:

      • 感谢您的回复,我不知道 $exist 如何帮助通过多个元素搜索集合,在我的情况下,所有列都已经存在
      • 当您搜索特定名称时,您可以对所需的每个字段使用:{'name': 'aaa'} 等。
      猜你喜欢
      • 2022-10-25
      • 1970-01-01
      • 2022-10-24
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多