MongoDB通过动态查询第N个元素来获取数据集合

时间:2016-05-12 15:42:00

标签: javascript meteor

我有一张动态的票证状态。每张票可以处于状态" 处理中"并且有不同的" this.statusStack.currentStatus "值。

下一个代码(不起作用)试图解释我尝试做什么

var tickets = Tickets.find({
  driverAsigned: Meteor.userId(),
  'statusStack.stack.(this.statusStack.currentStatus).name': "In Process"
});

*****新数据******

我的意思" this.statusStack.currentStatus "是一个内部文档值,而不是我的代码的变量。

这是票证的结构

{
......
"statusStack" : {
    "stack" : [
        {
            "sortable" : false,
            "removable" : false,
            "order" : 0,
            "name" : "Draft",
            "customName" : "Draft",
            "description" : "When the ticket has been created",
            "color" : "#4d4d4d"
        },
        {
            "sortable" : false,
            "removable" : false,
            "order" : 1,
            "name" : "New",
            "customName" : "New",
            "description" : "When the ticket info has been updated",
            "color" : "#f4f93e"
        },
        {
            "sortable" : true,
            "removable" : false,
            "order" : 2,
            "name" : "Assigned",
            "customName" : "Assigned",
            "description" : "When the ticket info has been asigned to a driver",
            "color" : "#3e89f9"
        },
        {
            "sortable" : true,
            "removable" : false,
            "order" : 3,
            "name" : "In Process",
            "customName" : "In Process",
            "description" : "When the ticket has been created",
            "color" : "#f0883c"
        },
        {
            "sortable" : true,
            "removable" : true,
            "order" : 4,
            "name" : "Frodo",
            "customName" : "Blank",
            "description" : "New Blank Status",
            "color" : "#4d4d4d"
        },
        {
            "sortable" : true,
            "removable" : false,
            "order" : 5,
            "name" : "Resolved",
            "customName" : "Resolved",
            "description" : "When the Driver has resolved the Ticket",
            "color" : "#42f93e"
        },
        {
            "sortable" : false,
            "removable" : false,
            "order" : 6,
            "name" : "Closed",
            "customName" : "Closed",
            "description" : "When the Admin has been closed the Ticket",
            "color" : "#217b1f"
        }
    ],
    "currentStatus" : 5
},
......
}

1 个答案:

答案 0 :(得分:0)

Sudo Code:

  1. 获取数组元素位置优先。

    pos = statusStack.currentStatus

  2. 然后您可以使用该pos(int)变量来构建查询文档

    var query = tojson(' {" statusStack.stack。' + pos' +' .name":" In Process" }&#39);

  3. 在查找中使用该查询文档。