0

Hi i want to find the elements of array. I think if you see the db and expected data, you can understand my problem.

This is my db.

This is example.

{
    member_id : "aeraew",
    name....
    ..
    items : [
        {
          title : "a",
          category : "1",
          method : "1",
          money : "1",
          memo : "memo",
          date : "2017-01-02"
        },
        {
          title : "b",
          category : "1",
          method : "1",
          money : "1",
          memo : "memo",
          date : "2017-01-02"
        },
        {
          title : "c",
          category : "1",
          method : "1",
          money : "1",
          memo : "memo",
          date : "2017-01-03"
        }  

    ]

}

My expect is :

    {
      title : "a",
      category : "1",
      method : "1",
      money : "1",
      memo : "memo",
      date : "2017-01-02"
    },
    {
      title : "b",
      category : "1",
      method : "1",
      money : "1",
      memo : "memo",
      date : "2017-01-02"
    },
    _id : "ididid"
}

$elemMatch find only first element, and $in "items.date" : "2017-01-01" etc.. is not working.

What i can do ?

suk
  • 23
  • 3

1 Answers1

0
db.collection.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $unwind: {
                path: '$items'
            }
        },

        // Stage 2
        {
            $match: {
                'items.date': "2017-01-02"
            }
        },

        // Stage 3
        {
            $group: {
                _id: '$_id',
                items: {
                    $addToSet: '$items'
                }
            }
        },

    ]



);
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26