搜尋

首頁  >  問答  >  主體

mongodb陣列查詢 - MongoDB 查詢時,如何篩選子文件?

有一個文檔,裡麵包含一個子文檔,大致如下:

{
    "_id" : ObjectId("59376b35a233be856d4a0731"),
    "plate_number" : "京A11111",
    "breakdown" : [
        {
            "file_id" : "4003732",
            "case_number" : "xxxx",
            "lev_code" : "B231",
            "crdate" : ISODate("2016-01-19T17:00:43Z")
        },
        {
            "file_id" : "4010523",
            "case_number" : "xxxx",
            "lev_code" : "B231",
            "crdate" : ISODate("2016-06-29T14:53:36Z")
        },
        {
            "file_id" : "4010980",
            "case_number" : "xxxx",
            "lev_code" : "B211",
            "crdate" : ISODate("2016-07-01T17:18:40Z")
        }
    ]
}

現在的需求是,如何使用PHP 7根據plate_number & lev_code查詢來匹配的個數

還有個問題是,如何能匹配符合條件的子文檔?

# mongo
find({plate_number:"京A11111", breakdown.lev_code:"B231"})
{
    "breakdown" : [
        {
            "file_id" : "4003732",
            "case_number" : "xxxx",
            "lev_code" : "B231",
            "crdate" : ISODate("2016-01-19T17:00:43Z")
        },
        {
            "file_id" : "4010523",
            "case_number" : "xxxx",
            "lev_code" : "B231",
            "crdate" : ISODate("2016-06-29T14:53:36Z")
        },
        {
            "file_id" : "4010980",
            "case_number" : "xxxx",
            "lev_code" : "B211",
            "crdate" : ISODate("2016-07-01T17:18:40Z")
        }
    ]
}
阿神阿神2786 天前957

全部回覆(1)我來回復

  • 怪我咯

    怪我咯2017-06-10 09:50:08

    建議使用MongoDB的aggreation的$filter:

    https://docs.mongodb.com/mast...

    Love MongoDB! Have fun!

    回覆
    0
  • 取消回覆