Mongodb unwind nested documents

前端 未结 1 1321
刺人心
刺人心 2020-12-19 06:08

I have a document in MongoDB and I\'m trying to unwind it in PHP. I want to unwind a document that has a subdocument that contains yet anothersubdocument. I was able to do t

相关标签:
1条回答
  • 2020-12-19 06:26

    I know that what you're trying to do is possible with MongoDB. The aggregate() command can take as many arguments as you need.

    In the mongo shell, a command like this

    db.collection.aggregate(
      { $project: {
        _id: 1,
        items: 1
      } },
      { $unwind: '$items' },
      { $unwind: '$items.images' }
    );
    

    will unwind the items subdocument, then the images subdocument.

    Based on the code in your question, perhaps this will work

    $project = array(
      '$project' => array(
        '_id' => 1,
        'items' => 1,
      )
    );
    
    $unwind_items = array(
      '$unwind' => '$items'
    );
    
    $unwind_images = array(
      '$unwind' => '$items.images'
    );
    
    
    $query = $mongo->store->aggregate($project,$unwind_items,$unwind_images);
    
    0 讨论(0)
提交回复
热议问题