2016-05-12 76 views
2

我有一個動態的票證狀態堆棧。其中每張票可以處於狀態「處理中」並具有不同的「this.statusStack.currentStatus」值。MongoDB通過動態查詢第N個元素來獲取數據收集

下一個代碼(即不工作)試圖解釋什麼,我試圖做

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 
}, 
...... 
} 
+2

您能否爲門票收藏提供示例文檔,並且更詳細地說明您需要什麼。 –

+0

完成@LaharShah –

回答

0

須藤代碼的結構:

  1. 獲取數組元素位置的第一。

    POS = statusStack.currentStatus

  2. 然後可以使用該POS(int)的變量來構建查詢文檔

    變種查詢=的toJSON( '{「statusStack.stack。 '+ POS' +' .name「:」In Process「}');

在find中使用該查詢文檔。

+0

我在試圖避免查詢所有不需要的票。 如果我照你說的我會檢索所有的門票,然後遍歷只得到那些我想 'VAR門票= Tickets.find({driverAsigned:Meteor.userId()});' 'tickets.forEach (函數(票){ 如果(ticket.statusStack.stack [ticket.statusStack.currentStatus]。名稱=== 「處理」){// 做我想做 } });' 我哪考慮有點沉重 –

+0

我同意...調查聚合框架,看看你是否找到任何方式使用它。聚合框架提供了許多額外的東西,我們可以使用。 –

相關問題