动态创建mongo查询对象

时间:2017-05-11 11:00:00

标签: javascript json mongodb

我有一个mongo查询,我想从我的javascript代码中动态运行

我想通过向查询提供实体和实体ID来动态创建查询

鉴于我有以下映射

Response Code: HTTP/1.1 400 Bad Request
Response: 
<html><body><error>Deployment tried with both 'packaging' and/or 'extension' being empty! One of these values is mandatory!</error></body></html>`

因此,当我传递实体(order,client或clientRelationship)的值时,我应该从EntityMapping和EntityIdMapping获取一个层次结构,然后我可以在查询中使用

var EntityMapping = { 
    order: "orderState",
        client: "client",
        clientRelationship: "clientRelationship"
};

var EntityIdMapping = { 
    order: "orderId",
        client: "ctnId",
        clientRelationship: "crIdentification"
};

1 个答案:

答案 0 :(得分:1)

您希望使用动态密钥 a b 创建嵌套对象查询。除非您首先初始化第一级子对象,否则您将收到错误消息,指出 query [a] 未定义且没有名为 b 的属性。

function btnclick(data) {
    console.log('btnclick(data)');
    console.log(data);

    var treeViewInstance = $('#RolesTreeView').dxTreeView('instance');
    //var itemElement = treeViewInstance.element().find("[data-item-id='" + args.itemData.RoleId + "'] > .dx-treeview-item").get(0);
    var itemElement = treeViewInstance.element().find("[data-item-id='10']  > .dx-treeview-item").get(0);
    treeViewInstance.expandItem(itemElement);
    treeViewInstance.selectItem(itemElement);
}

使用ES6,这会更简单(http://es6-features.org/#ComputedPropertyNames):

var query = {};
query[a] = {}; // Initialize top-level child object
query[a][b] = 45;