如何基于猫鼬中的id从对象数组中获取一个对象

时间:2020-09-23 03:52:51

标签: mongodb mongoose

我有这样的数据:

---
    - name: setup mysql
      docker_container:
        name: "{{ dockers.mysql.name }}"
        image: "{{ dockers.mysql.image }}"
        volumes:
          - "{{ dockers.mysql.volume }}:{{ dockers.mysql.volume_mount }}"
        state: started
        restart: yes
        restart_policy: "{{ dockers.mysql.restart }}"
        memory: "{{ dockers.mysql.mem }}"
        networks:
            - name: {{ docker_network_name }}
        env:
          MYSQL_ROOT_PASSWORD: "{{ MYSQL_ROOT_PASSWORD }}"
          MYSQL_DATABASE: "{{ MYSQL_DATABASE }}"
          MYSQL_USER: "{{ MYSQL_USER }}"
          MYSQL_PASSWORD: "{{ MYSQL_PASSWORD }}"
      tags: [ 'docker-run' ]
    
    - name: setup wordpress
      docker_container:
        name: "{{ dockers.wordpress.name }}"
        image: "{{ dockers.wordpress.image }}"
        volumes:
          - "{{ dockers.wordpress.volume2 }}:{{ dockers.wordpress.volume_mount2 }}"
          - "{{ dockers.wordpress.volume }}:{{ dockers.wordpress.volume_mount }}"      
        state: started
        restart: yes
        restart_policy: "{{ dockers.wordpress.restart }}"
        memory: "{{ dockers.wordpress.mem }}"
        ports:
         - "{{ dockers.wordpress.ports }}"
      
        networks:
           - name: {{ docker_network_name }}
        env:
          #WORDPRESS_DB_HOST: "{{ WORDPRESS_DB_HOST | default(omit) }}"  #also tried: "mysql:3306"
          WORDPRESS_DB_USER: "{{ WORDPRESS_DB_USER }}"
          WORDPRESS_DB_PASSWORD: "{{ WORDPRESS_DB_PASSWORD }}"
          WORDPRESS_DB_NAME: "{{ WORDPRESS_DB_NAME }}"
      tags: [ 'docker-run' ]

我需要基于{ "global": { "summaryNotificationSchedule": { "weekDay": ["wednesday"] } }, "userEmail": "test1@gmail.com", "environments": [ { "userPreference": { "summaryNotificationSchedule": { "weekDay": ["friday"] } }, "envId": "u1" }, { "userPreference": { "summaryNotificationSchedule": { "weekDay": ["tuesday"], } }, "envId": "u2" } ] } userPreference数组中获得environments个对象。

对此进行了尝试,但是它返回的是完整的数组,但是我需要在数组内返回对象,即envId

userPreference

2 个答案:

答案 0 :(得分:1)

您可以使用位置运算符。但要注意使用postional operator in projection

db.collection.find({
  "environments.envId": "u2"
},
{
  "environments.userPreference.$": 1
})

工作Mongo playground

更新1,

输出为您所期望的

[
  {
    $project: {
      environments: {
        $filter: {
          input: "$environments",
          cond: {
            $eq: [
              "$$this.envId",
              "u1"
            ]
          }
        }
      }
    }
  },
  {
    $project: {
      userPreference: {
        $ifNull: [
          {
            $arrayElemAt: [
              "$environments.userPreference",
              0
            ]
          },
          {}
        ]
      }
    }
  }
]

工作Mongo playground

答案 1 :(得分:1)

给出以下建议:

查找查询:

db.collection.findOne(
    {
        userEmail: "test1@gmail.com",
    },
    {
        environments: {
            $elemMatch: {
                envId: "u1"
            }
        }
    }
)

聚合管道:

db.collection.aggregate([
    {
        $match: {
            userEmail: "test1@gmail.com"
        }
    },
    {
        $set: {
            environments: {
                $first: {
                    $filter: {
                        input: "$environments",
                        cond: { $eq: ["$$this.envId", "u1"] }
                    }
                }

            }
        }
    },
    {
        $replaceWith: "$environments.userPreference"
    }
])