【问题标题】:How To Structure Dynamic MongoDb Query如何构建动态 MongoDb 查询
【发布时间】:2016-09-07 14:35:54
【问题描述】:

请让我想一想如何动态查询集合。我有一个具有以下架构的集合。

Tripart_Property_Schema = new SimpleSchema({
    "type" : {
        type : String,
        label : 'Property type',    
    },

    "sale_lease" : {
        type : String,
        label : '', 
    },

    "location" : {
        type : Object,
        label : '', 
        optional : true
    },

    "location.state" : {
        type : String,
        label : 'state',    
    },

    "location.lga" : {
        type : String,
        label : 'lga',
        optional : true 
    },

    "location.address" : {
        type : String,
        label : 'address',
        optional : true 
    },

    "amount" : {
        type : Object,
        label : '',
        optional : true
    },

    "amount.amount" : {
        type : Number,
        label : '',
        optional : true
    },

    "amount.discount" : {
        type : Number,
        label : '',
        optional : true
    },

    "active" : {
        type : Boolean,
        label : '',
        optional : true
    },

    "views" : {
        type : Number,
        label : '',
        optional : true
    },

    "date_added" : {
        type : Date ,
        label : '',
        optional : true
    },

    "general_features" : {
        type : [String],
        label : '',
        optional : true
    },

    "outdoor_features" : {
        type : [String],
        label : '',
        optional : true
    },

    "indoor_features" : {
        type : [String],
        label : '',
        optional : true
    },

    "other_facilities" : {
        type : Object,
        label : '',
        optional : true

    },

    "other_facilities.bedroom" : {
        type : Number,
        label : '',
        optional : true 
    },

    "other_facilities.bathroom" : {
        type : Number,
        label : ' ',
        optional : true 
    },

    "other_facilities.garage" : {
        type : Number,
        label : '',
        optional : true 
    },

    "other_facilities.staffQuaters" : {
        type : Number,
        label : '',
        optional : true 
    }
});

我创建了一个用户界面,用户可以在其中使用可用字段的任意组合查询数据。用户可以使用sale_lease 字段、amount.amount 字段查询属性,也可以查询数组general_features 字段。我不知道如何根据用户选择的偏好生成此查询。我知道如何在事先知道查询的字段的情况下执行查询,但使其动态化是问题所在。

我正在尝试使用多个if 语句来查询所有可能的字段组合,但我有点意识到这不是实现这一目标的方法。如果我能指出正确的方向,我将不胜感激。

【问题讨论】:

标签: mongodb meteor dynamic-queries


【解决方案1】:

让我们以一个简单的例子为例,您的集合有多个键,用户可以查询它们的任意组合,并且您想要 AND 结果。一个典型的模式是:

let query = {}'
if ( $("#type").length ) query.type = $("#type").val();
if ( $("#sale_lease").length ) query.sale_lease = $("#sale_lease").val();
if ( $("#state").length ) query.location = { state: $("#state").val() };
return Tripart_Property.find(query);

为简单起见,以上假设您已为每个搜索字段指定了一个与相应架构字段相同的 ID。您的命名约定可能会有所不同。

如您所见,您从一个空白查询对象开始,然后查看每个搜索字段,如果它是非空白的,那么您将一个键添加到与您要搜索的架构键对应的查询中。

通过良好的命名约定和简单的字段结构,您甚至可以在 js 循环中执行此操作,因此您不需要每个键都有一个 if 语句。

【讨论】:

  • 感谢@Michel Floyd,您的建议有效,但我在查询其值为对象的键时遇到了一些麻烦。就像使用我的模式 location.state。请您更新您的答案以反映这一点,以便我可以接受它作为答案。
【解决方案2】:

在不知道用户如何为查询做出选择、集合名称等细节的情况下,很难提供详细的示例;但是,您应该能够获取用户的选择并将其转换为查询参数对象。例如,假设集合名称是 Tripart_Property,如果用户选择 sale_lease 字段,您可以执行以下操作:

var saleLeaseSelection = argumentContainingSelectionFromUser;
var customQuery = { sale_lease: saleLeaseSelection };
Tripart_Property.find(customQuery);

这是一个非常基本的例子。 argumentContainingSelectionFromUser 是来自用户选择的数据。您可能希望在服务器上构建一个可以从客户端调用的方法,该方法将返回查询结果,以便您可以将它们显示给用户,但这至少应该让您指向正确的方向。

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2012-12-31
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2013-12-03
    相关资源
    最近更新 更多