0

我有以下文檔:MongoDB的多維數組拼合聚集

{ 
"items" : [ 
    [ 
     { 
      "itemID" : "arSiPKXhqAXQ8MwMA", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(56) 
     }, 
     { 
      "itemID" : "xmuZT4u7o6YPQ87Bf", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(55) 
     } 
    ], 
    [ 
     { 
      "itemID" : "QjvcodRRK8dBeAzyh", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(80), 
      "side" : NumberInt(147), 
      "projected" : NumberInt(68) 
     }, 
     { 
      "itemID" : "jaQ3o6ba6dwNv38Bm", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(98), 
      "side" : NumberInt(138), 
      "projected" : NumberInt(88) 
     } 
    ], 
    [ 
     { 
      "itemID" : "ZPrSr5LZJt7mQP9KM", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(141), 
      "side" : NumberInt(138), 
      "projected" : NumberInt(70) 
     }, 
     { 
      "itemID" : "qR5qsyzfbAgqcvA84", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(69), 
      "side" : NumberInt(148), 
      "projected" : NumberInt(99) 
     } 
    ], 
    [ 
     { 
      "itemID" : "XrTEua9rRgieGyDo4", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(62) 
     }, 
     { 
      "itemID" : "GT4d9kdNSn9AikjBF", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(62) 
     } 
    ], 
    [ 
     { 
      "itemID" : "cPEN7fnt9CG3zAFES", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(104) 
     }, 
     { 
      "itemID" : "2z4KER4JR8iY3CAst", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(57) 
     } 
    ], 
    [ 
     { 
      "itemID" : "Csq6DbLrmFA4h8FdS", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(78) 
     }, 
     { 
      "itemID" : "4BpZPss5ueJHYg2g9", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(108) 
     } 
    ], 
    [ 
     { 
      "itemID" : "abMCndutq2gMtMpC3", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(109), 
      "side" : NumberInt(50), 
      "projected" : NumberInt(103) 
     }, 
     { 
      "itemID" : "sZTtYi8AMrXWRyHM2", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(128), 
      "side" : NumberInt(66), 
      "projected" : NumberInt(87) 
     } 
    ], 
    [ 
     { 
      "itemID" : "6bwLiurf5D5oziLmF", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(125), 
      "side" : NumberInt(79), 
      "projected" : NumberInt(75) 
     }, 
     { 
      "itemID" : "esmvcWMeNbZXzFjSg", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(98), 
      "side" : NumberInt(132), 
      "projected" : NumberInt(51) 
     } 
    ], 
    [ 
     { 
      "itemID" : "AibkKHbpdESHM47fv", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(67), 
      "side" : NumberInt(82), 
      "projected" : NumberInt(72) 
     }, 
     { 
      "itemID" : "TpsaYTST536hm4BLx", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(148), 
      "side" : NumberInt(92), 
      "projected" : NumberInt(69) 
     } 
    ], 
    [ 
     { 
      "itemID" : "R8iNca43bTc97DKgG", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(50) 
     }, 
     { 
      "itemID" : "v7x4yKFFF5vPjLTWb", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(63) 
     } 
    ], 
    [ 
     { 
      "itemID" : "XAFpcujePnKHMq3et", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(124), 
      "side" : NumberInt(87), 
      "projected" : NumberInt(50) 
     }, 
     { 
      "itemID" : "BLA2azuv2FboStGKo", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(122), 
      "side" : NumberInt(83), 
      "projected" : NumberInt(71) 
     } 
    ], 
    [ 
     { 
      "itemID" : "2urB3BRLjZWjaHrEp", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(72) 
     }, 
     { 
      "itemID" : "B5Nurs4CXmzQnQ9Zg", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(78) 
     } 
    ], 
    [ 
     { 
      "itemID" : "itjB327N8drTZxKx9", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(84) 
     }, 
     { 
      "itemID" : "6YzDgcvkfoAf9nLpf", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(107) 
     } 
    ], 
    [ 
     { 
      "itemID" : "AdhS2TuHj93eyQ8mC", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(71) 
     }, 
     { 
      "itemID" : "NSBbbKqioTCfC3yAp", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(51) 
     } 
    ], 
    [ 
     { 
      "itemID" : "vZXifWuiDeTgJiR8z", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(60) 
     }, 
     { 
      "itemID" : "y5SLnS5qjKQWcNqC3", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(73) 
     } 
    ], 
    [ 
     { 
      "itemID" : "K8i4FveNeDMknuHe2", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(95), 
      "side" : NumberInt(139), 
      "projected" : NumberInt(94) 
     }, 
     { 
      "itemID" : "jfRqG7o6GvHxqR7XB", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(52), 
      "side" : NumberInt(67), 
      "projected" : NumberInt(62) 
     } 
    ], 
    [ 
     { 
      "itemID" : "AeWX8ycx8Kxx5fs9j", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(58) 
     }, 
     { 
      "itemID" : "zB4fvrkHtKuLqfFdT", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(107) 
     } 
    ], 
    [ 
     { 
      "itemID" : "k4MAXcqv6M4CNbyqK", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(53) 
     }, 
     { 
      "itemID" : "sKtNyWfp8LQmFdDhg", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(106) 
     } 
    ], 
    [ 
     { 
      "itemID" : "WxEmSxJzmiuiiZF6e", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(63) 
     }, 
     { 
      "itemID" : "DJdncCq5apwr8RZbg", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(77) 
     } 
    ], 
    [ 
     { 
      "itemID" : "hRpGE9RBt8zKuGNWo", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(89) 
     }, 
     { 
      "itemID" : "5eYvckNjWL6QnnroF", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(87) 
     } 
    ], 
    [ 
     { 
      "itemID" : "kCugbteCN3zYnpsQe", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(72) 
     }, 
     { 
      "itemID" : "cBmT5HHQSfGHsjxW4", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(79) 
     } 
    ], 
    [ 
     { 
      "itemID" : "N7xzob86NNDtzti5W", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(92) 
     }, 
     { 
      "itemID" : "j4xrE2gaKdJYv2BuN", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(102) 
     } 
    ], 
    [ 
     { 
      "itemID" : "jhnr5wNiRQFR2fCZy", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(103) 
     }, 
     { 
      "itemID" : "JYaJZhYcrFnzQAwQ2", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(94) 
     } 
    ], 
    [ 
     { 
      "itemID" : "sc7pYWMhKfHZLydCC", 
      "name" : "Item number 1", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(53) 
     }, 
     { 
      "itemID" : "K9qTTmeWHPt8d7Zwy", 
      "name" : "Item number 2", 
      "complexDrink" : false, 
      "real" : NumberInt(0), 
      "side" : NumberInt(0), 
      "projected" : NumberInt(99) 
     } 
    ] 
], 

}

我想返回所有的項目在1個陣列,而不使用$unwind。我必須在$project階段做到這一點。我會怎麼做?我試圖

db.getCollection.aggregate([ 
{ 
    $match: { 
    registerID: { $in: [ObjectId("7e343dadb3ded2eb83db3bb0"), ObjectId("018039aab7a39d32b573ae43")] }, 
    timestamp: { 
     $gte: ISODate('2017-01-10T00:00:00+01:00'), 
     $lte: ISODate('2017-01-10T23:59:59+01:00'), 
    } 
    } 
}, 
     { 
     $group: { 
      _id: '$registerID', 
      items: { $push: '$items' } 
     }, 
     }, 
     { 
     $project: { 
      'items': { 
      $map: { 
       input: '$items', 
       as: 'item', 
       in: '$$item' 
      } 
      }, 
     }, 
    }, 
     { 
     $sort: { 
      timestamp: 1, 
     }, 
     }, 
]) 

回答

0

添加這個階段您的管道

{ 
    items: { 
      $map: { 
       input: "$items", 
       as: "itemList", 
       in: {$arrayElemAt: ["$$itemList", 0]} 
      } 
    } 
}