2016-09-29 9 views
0

MongoDBでNodejsバックエンドを使用して、MongooseをORMとしてオブジェクト構造を保持しています。
MongoDBドキュメントコレクション内のオブジェクト配列内の要素を検索

私は以下のパターンに従う作業オーダーオブジェクトを持っています。何千もの作業指示書が生成されており、特定の作業者に割り当てられた作業指示書を特定の日付に見つける必要があります。

検索クエリを思いついてもらえますか?

クエリが

  1. 照会とitenary見つけることができるはずです詳細 - 割り当てられた作業指示 - 指定された日付
  2. 内の指定された労働者のための詳細はallocatedWorker(労働者)とallocatedDateTime(日付を使用する必要がありますそして

Work orderオブジェクト

と一致し、見つけるための時間)
{ 
    customer:  String, 
    address:  String, 
    /** 
    * List of to do items. Each item will mainly belong in to a given category 
    * and a collection of tasks. 
    * The task can be system generated, admin assigned or customer raised. 
    */ 
    todo: [ 
     { 
      category: String, 

      /** 
      * A collection of raised tasks 
      */ 
      tasks: [ 
       { 
        /** 
        * Holds the customer request details 
        */ 
        request: { 
         workItem:   String, 
         instructions:  String, 

         /** 
         * Preferred date and time 
         */ 
         dateTime:   String, 
         /** 
         * Preferred worker if any 
         */ 
         preferredWorker: String, 
         receivedDateTime: String 
        }, 

        /** 
        * Details of the schedule 
        */ 
        schedule: [ 
         { 
          /** 
          * Allocated worker's user name 
          */ 
          allocatedWorker: String, 
          allocatedDateTime: String, 
          allocatedDuration: Number, 
          /** 
          * Schedule changed date and time 
          */ 
          scheduledOn:  String, 
          /** 
          * Scheduler's user name 
          */ 
          scheduledBy:  String, 
          /** 
          * Scheduled task status 
          * 0: Pending - new 
          * 1: Scheduled - allocated 
          * 2: Work In Progress 
          * 3: Completed 
          * 4: Overdue 
          */ 
          itemStatus:   Number 
         } 
        ], 

        /** 
        * Stage of the work order 
        * 0: Pending - new 
        * 1: Scheduled - allocated 
        * 2: Work In Progress 
        * 3: Completed 
        * 4: Overdue 
        */ 
        status:     Number 
       } 
      ] 
     } 
    ] 
} 
+0

これは私が試したものです。 'Workorder.find({$ and:{{' todo.tasks.schedule.allocatedWorker ':worker}、{' todo.tasks.schedule.allocatedDateTime ':day}]}関数(err、orders){}) ' – Aviro

答えて

1

は、クエリの下に試してみてください。

Workorder.find({'todo.tasks.schedule.allocatedWorker'‌​: worker, 
'todo.tasks.schedule.allocatedDateTime': day}, function(err, orders) {}); 

は、両方のは、DBに保存された正確な値が一致する(正確な名前と日時を)やるということを覚えておいてください。

関連する問題