简体   繁体   中英

How to Convert MYSQL Query to Mongodb Aggregation

how to convert mysql query to mongodb aggregation

my database in mysql

在此处输入图像描述

i need to convert mysql to mongodb query

SELECT * FROM `auctions` WHERE ( (status!='SCHEDULED' AND status='FAILED') OR (status!='RUNNING' AND status='FAILED') ) GROUP BY auctioncode

in this query auctioncode and status are wise record needed auctioncode is multiple records. i need status='FAILED' that auctioncode status not in SCHEDULED or UNPAID

Something like below can be done pls refer here for example query: https://mongoplayground.net/p/8zHYhXvbkxs

db.auctions.aggregate([
  {
    "$match": {
      "$or": [
        {
          "$and": [
            {
              status: {
                $ne: "SCHEDULED"
              }
            },
            {
              status: "FAILED"
            }
          ]
        },
        {
          "$and": [
            {
              status: {
                $ne: "RUNNING"
              }
            },
            {
              status: "FAILED"
            }
          ]
        }
      ]
    }
  },
  {
    "$group": {
      "_id": "$auctioncode",
      "auctioncode": {
        "$first": "$auctioncode"
      },
      "Details": {
        "$addToSet": "$$ROOT"
      }
    }
  }
])

Pipeline stages: match: to filter based on your requirement: group: to do group by auctioncode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM