收藏
回答

小程序云开发中云数据库where中怎么查询某个数组下符合某个条件的数据?

{

  _id:face13585d4930ed0d0ea60c318aa616

  oils:[

    {

      oil_type:'92#',

      price: 7.92

    },

    {

      oil_type:'95#',

      price: 8.26

    },

    {

      oil_type:'98#',

      price: 9.04

    },

  ],

  station_id: 'W9yHmau9e31Z7KqO'

},
{

  _id:face13585d4930ed0d0ea60c34345

  oils:[

    {

      oil_type:'92#',

      price: 7.82

    },

    {

      oil_type:'95#',

      price: 8.26

    },

    {

      oil_type:'98#',

      price: 9.04

    },

  ],

  station_id: 'H9yHmau9e3342HS12a'

},



上面是我某个数据表'oil_prices'中的数据,目前暂且只有2条,这时候我想通过根据station_id和oils数组中的oil_type进行查询到对应数据,然后更新对应的字段数据,能做到吗? 以下写法貌似没效果。

db.collection('oil_prices').where({
        station_id: event.stationId,
        'oils.$[].oil_type': event.oilType
      }).update({
        data: {
          'oils.$[].price': event.price
        },
        success: function (res) {
          return{
            code:200,
            msg:'油价更新成功'
          }
        }
      })


回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容
问题标签