Фильтрация на монго-документе и конкретных полях проекта - PullRequest
0 голосов
/ 08 ноября 2019

Я довольно новичок в MongoDb. Много пробовал, но застрял на одном запросе. Пожалуйста, найдите образец монго-документа по указанному ниже:

{ 
    "_id" : ObjectId("5dc158a60566e14c5190db72"), 
    "hotel_id" : NumberInt(45), 
    "plans" : [
        {
            "plan_type" : "AP", 
            "sub_plans" : [
                {
                    "channels" : [
                        {
                            "channel_name" : "default", 
                            "status" : "Pending", 
                            "is_active" : false, 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "non_veg_available" : null, 
                            "oyo_commission" : 20.0, 
                            "ep_price_reduction" : null, 
                            "salesforce_id" : "a0Y280000023LFwEAM", 
                            "breakfast_start_time" : null, 
                            "breakfast_end_time" : null, 
                            "lunch_start_time" : null, 
                            "lunch_end_time" : null, 
                            "dinner_start_time" : null, 
                            "dinner_end_time" : null, 
                            "created_at" : NumberLong(1468577718000), 
                            "updated_at" : NumberLong(1481074321000), 
                            "prices" : [
                                {
                                    "occupancy" : NumberInt(1), 
                                    "guest_type" : null, 
                                    "veg_price" : NumberInt(400), 
                                    "non_veg_price" : null, 
                                    "additional_non_veg_price" : null, 
                                    "discount" : null
                                }
                            ]
                        }, 
                        {
                            "channel_name" : "default", 
                            "status" : "Pending", 
                            "is_active" : false, 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "non_veg_available" : null, 
                            "oyo_commission" : 20.0, 
                            "ep_price_reduction" : null, 
                            "salesforce_id" : "a0W28000004GqL4", 
                            "breakfast_start_time" : "7:30", 
                            "breakfast_end_time" : "10:00", 
                            "lunch_start_time" : "12:30", 
                            "lunch_end_time" : "15:00", 
                            "dinner_start_time" : "19:00", 
                            "dinner_end_time" : "22:30", 
                            "created_at" : NumberLong(1505516604000), 
                            "updated_at" : NumberLong(1505516604000), 
                            "prices" : [
                                {
                                    "occupancy" : NumberInt(1), 
                                    "guest_type" : null, 
                                    "veg_price" : NumberInt(425), 
                                    "non_veg_price" : NumberInt(425), 
                                    "additional_non_veg_price" : 0.0, 
                                    "discount" : null
                                }
                            ]
                        }, 
                        {
                            "channel_name" : "default", 
                            "status" : "Pending", 
                            "is_active" : false, 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "non_veg_available" : null, 
                            "oyo_commission" : 20.0, 
                            "ep_price_reduction" : null, 
                            "salesforce_id" : "a0W28000004GqL4", 
                            "breakfast_start_time" : "7:30", 
                            "breakfast_end_time" : "10:00", 
                            "lunch_start_time" : "12:30", 
                            "lunch_end_time" : "15:00", 
                            "dinner_start_time" : "19:00", 
                            "dinner_end_time" : "22:30", 
                            "created_at" : NumberLong(1505709978000), 
                            "updated_at" : NumberLong(1542162045000), 
                            "prices" : [
                                {
                                    "occupancy" : NumberInt(1), 
                                    "guest_type" : null, 
                                    "veg_price" : NumberInt(425), 
                                    "non_veg_price" : NumberInt(425), 
                                    "additional_non_veg_price" : 0.0, 
                                    "discount" : null
                                }
                            ]
                        }
                    ], 
                    "sub_plan_type" : "Standard", 
                    "is_sub_plan_default" : false
                }
            ]
        }, 
        {
            "plan_type" : "EP", 
            "sub_plans" : [
                {
                    "channels" : [
                        {
                            "channel_name" : "default", 
                            "status" : "Pending", 
                            "is_active" : false, 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "non_veg_available" : null, 
                            "oyo_commission" : null, 
                            "ep_price_reduction" : NumberInt(80), 
                            "salesforce_id" : "a0Y280000023LG1EAM", 
                            "breakfast_start_time" : null, 
                            "breakfast_end_time" : null, 
                            "lunch_start_time" : null, 
                            "lunch_end_time" : null, 
                            "dinner_start_time" : null, 
                            "dinner_end_time" : null, 
                            "created_at" : NumberLong(1468577718000), 
                            "updated_at" : NumberLong(1481074321000), 
                            "prices" : [
                                {
                                    "occupancy" : NumberInt(1), 
                                    "guest_type" : null, 
                                    "veg_price" : null, 
                                    "non_veg_price" : null, 
                                    "additional_non_veg_price" : null, 
                                    "discount" : null
                                }
                            ]
                        }
                    ], 
                    "sub_plan_type" : "Standard", 
                    "is_sub_plan_default" : false
                }
            ]
        }, 
        {
            "plan_type" : "MAP", 
            "sub_plans" : [
                {
                    "channels" : [
                        {
                            "channel_name" : "default", 
                            "status" : "Pending", 
                            "is_active" : false, 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "non_veg_available" : null, 
                            "oyo_commission" : 20.0, 
                            "ep_price_reduction" : null, 
                            "salesforce_id" : "a0Y280000023LG6EAM", 
                            "breakfast_start_time" : null, 
                            "breakfast_end_time" : null, 
                            "lunch_start_time" : null, 
                            "lunch_end_time" : null, 
                            "dinner_start_time" : null, 
                            "dinner_end_time" : null, 
                            "created_at" : NumberLong(1468577718000), 
                            "updated_at" : NumberLong(1481074321000), 
                            "prices" : [
                                {
                                    "occupancy" : NumberInt(1), 
                                    "guest_type" : null, 
                                    "veg_price" : NumberInt(200), 
                                    "non_veg_price" : null, 
                                    "additional_non_veg_price" : null, 
                                    "discount" : null
                                }
                            ]
                        }, 
                        {
                            "channel_name" : "default", 
                            "status" : "Pending", 
                            "is_active" : false, 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "non_veg_available" : null, 
                            "oyo_commission" : 20.0, 
                            "ep_price_reduction" : null, 
                            "salesforce_id" : "a0W28000004GqL4", 
                            "breakfast_start_time" : "7:30", 
                            "breakfast_end_time" : "10:00", 
                            "lunch_start_time" : "12:30", 
                            "lunch_end_time" : "15:00", 
                            "dinner_start_time" : "19:00", 
                            "dinner_end_time" : "22:30", 
                            "created_at" : NumberLong(1505709978000), 
                            "updated_at" : NumberLong(1541476001000), 
                            "prices" : [
                                {
                                    "occupancy" : NumberInt(1), 
                                    "guest_type" : null, 
                                    "veg_price" : NumberInt(225), 
                                    "non_veg_price" : NumberInt(225), 
                                    "additional_non_veg_price" : 0.0, 
                                    "discount" : null
                                }
                            ]
                        }
                    ], 
                    "sub_plan_type" : "Standard", 
                    "is_sub_plan_default" : false
                }
            ]
        }
    ]
}

Я хочу только выбранные поля, если: идентификатор отеля 45, цена размещения 1, is_active равен false, статус "В ожидании" и тип плана в [«AP», «EP»]. Желаемый результат:

{ 
    "_id" : ObjectId("5dc158a60566e14c5190db72"), 
    "hotel_id" : NumberInt(45), 
    "plans" : [
        {
            "plan_type" : "AP", 
            "sub_plans" : [
                {
                    "channels" : [
                        {
                            "channel_name" : "default", 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "prices" : [
                                {
                                    "veg_price" : NumberInt(425)
                                }
                            ]
                        }, 
                        {
                            "channel_name" : "default", 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "prices" : [
                                {
                                    "veg_price" : NumberInt(425)
                                }
                            ]
                        }
                    ], 
                    "sub_plan_type" : "Standard", 
                    "is_sub_plan_default" : false
                }
            ]
        }, 
        {
            "plan_type" : "EP", 
            "sub_plans" : [
                {
                    "channels" : [
                        {
                            "channel_name" : "default", 
                            "start_date" : NumberLong(1468521000000), 
                            "end_date" : NumberLong(1901125800000), 
                            "prices" : [
                                {
                                    "veg_price" : null, 
                                }
                            ]
                        }
                    ], 
                    "sub_plan_type" : "Standard", 
                    "is_sub_plan_default" : false
                }
            ]
        }, 
    ]
}

Мой запрос:

db.collection.find({
  "hotel_id": 45,
  "plans.plan_type": {$in : ["AP", "EP"]},
  "plans.sub_plans.channels.prices.occupancy": 1,
  "plans.sub_plans.channels.is_active": false,
  "plans.sub_plans.channels.status": "Pending"
})

Примечание - мне нужно искать только в одном документе с идентификатором отеля 45. Пожалуйста, помогите. Заранее спасибо.

Ответы [ 2 ]

0 голосов
/ 08 ноября 2019

Возможно, вы ищете такой запрос.

db.collection.aggregate([
  {
    "$unwind": "$plans",

  },
  {
    "$group": {
      "_id": "$hotel_id",
      "hotel_id": {
        "$first": "$hotel_id"
      },
      "plans": {
        "$push": {
          "$cond": [
            {
              "$or": [
                {
                  "$eq": [
                    "$plans.plan_type",
                    "AP"
                  ]
                },
                {
                  "$eq": [
                    "$plans.plan_type",
                    "EP"
                  ]
                }
              ]
            },
            {
              "plan_type": "$plans.plan_type",
              "sub_plans": "$plans.sub_plans",
              "sub_plan_type": "$plans.sub_plan_type",
              "is_sub_plan_default": "$is_sub_plan_default"
            },
            null
          ]
        }
      }
    }
  },
  {
    "$project": {
      "_id": 1,
      "hotel_id": 1,
      "plans": {
        "$filter": {
          input: "$plans",
          as: "plan",
          cond: {
            $ne: [
              "$$plan",
              null
            ]
          }
        }
      }
    }
  }
])
0 голосов
/ 08 ноября 2019

Вам необходимо применить агрегацию.

db.collection_name.aggregate([

    {
        "$match":{
            "hotel_id": 45,
            "plans.plan_type": "AP",
            "plans.sub_plans.channels.prices.occupancy": 1,
            "plans.sub_plans.channels.is_active": false,
            "plans.sub_plans.channels.status": "Pending"
        }
    },
    {
        "$project":{
            "_id":"$_id",
            "hotel_id":"$hotel_id",
            "plans":"plans"
        }
    }
])

...