skip to Main Content

My documents look like this

 {
  "_id": {
    "$oid": "62825f71005ce00c5f0235c1"
  },
  "user": "jon",
  "roles": {
    "User": 2001,
    
  },
 
  "STOCK ": [
    {
      "sku": "BLACK-M",
      "productname": "BLACK",
      "sendout": 0,
      "recived": 1,
      "totalinstock": 40,
      "location": "B32",
      "_id": {
        "$oid": "62826016005ce00c5f0235c8"
      }
    },
  
    {
      "sku": "CJNS",
      "productname": "89796363",
      "sendout": 0,
      "recived": 45,
      "totalinstock": 0,
      "location": "B232",
      "_id": {
        "$oid": "62836f2d56b4f1ac79c99b8d"
      }
    }
  ],
   "ORDERS": [
    {
      "date": {
        "$date": "2022-06-02T15:23:58Z"
      },
      "OrderNumber": "745607",
      "City": "xxxxx",
      "Address": "yyyyyy",
      "Phone": "8678678678",
      "Name": "Holly ",
      "Trackingnumber": 40,
      "ZipCode": 00000,
      "Province": "New ",
      "Quantity": [
        1
      ],
      "Product_Name": [
        " pants pants"
      ],
      "SKU": [
        "CJNS"
      ],
      "_id": {
        "$oid": "6298d61ba6eeec72b78332a7"
      }
    },
    {
      "date": {
        "$date": "2022-06-02T15:23:58Z"
      },
      "OrderNumber": "748840",
      "City": "xxxxx",
      "Address": "yyyyyy",
      "Phone": "354353454",
      "Name": "Michael",
      "Trackingnumber": 0,
      "ZipCode": 00000,
      "Province": "North",
      "Quantity": [
        1
      ],
      "Product_Name": [
        " pants pants"
      ],
      "SKU": [
        "CJNS"
      ],
      "_id": {
        "$oid": "6298d61ba6eeec72b78332a9"
      }
    }
  ]
}

I successful to return all the objects in STOCK or all the objects in ORDERS
Through this query

const foundUser=  await User.find({"user":req.body.user},("Orders") ).exec()

Now I want to filter the response to include only items where "Trackingnumber" is different from 0

For the sample data I expect to receive only

{
      "date": {
        "$date": "2022-06-02T15:23:58Z"
      },
      "OrderNumber": "748840",
      "City": "xxxxx",
      "Address": "yyyyyy",
      "Phone": "354353454",
      "Name": "Michael",
      "Trackingnumber": 0,
      "ZipCode": 00000,
      "Province": "North",
      "Quantity": [
        1
      ],
      "Product_Name": [
        " pants pants"
      ],
      "SKU": [
        "CJNS"
      ],
      "_id": {
        "$oid": "6298d61ba6eeec72b78332a9"
      }
    }

2

Answers


  1. You can use an aggregation pipeline with a $filter for this:

    db.collection.aggregate([
      {
        $match: {
          "user": "jon"
        }
      },
      {
        $project: {
          ORDERS: {
            $filter: {
              input: "$ORDERS",
              as: "item",
              cond: {$ne: ["$$item.Trackingnumber", 0]}
            }
          }
        }
      }
    ])
    

    Playground example

    Login or Signup to reply.
  2. User.find({"Orders" : {"Trackingnumber": 0} })

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search