0

I am been trying to find an object in an array based on the key-value, from the data in MongoDB, below is the query from the database, but I only need some specific object, how can achieve that?

[
  {
    "_id": "627261a17acf7875b30d6e34",
    "orderItem": [
      [
        {
          "_id": "626fdb9fd0867e51d613530d",
          "productName": "Handmade jewelry",
          "productPrice": "25.99",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productimg4.jpg-1651497883882",
          "sellerId": "62470b37f2052b6a6463f9b3",
          "qty": 1
        },
        {
          "_id": "626fdbf4d0867e51d6135389",
          "productName": "Handmade bucket",
          "productPrice": "23.99",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/topSellingImg.jpg-1651497970510",
          "sellerId": "0062470b37f2052b6a6463f9b30",
          "qty": 2
        }
      ],
      [
        {
          "_id": "626a5d5dae8851064d4760ac",
          "productName": "handmade bag and show",
          "productPrice": "5",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823", "sellerId": "62470b37f2052b6a6463f9b3",

          "qty": 2
        },
        {
          "_id": "626a5dabae8851064d4760af",
          "productName": "hand bag",
          "productPrice": "20",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
          "qty": 1
        }
      ],
      [
        {
          "_id": "626a5d5dae8851064d4760ac",
          "productName": "handmade bag and show",
          "productPrice": "5",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
          "qty": 2
        },
        {
          "_id": "626a5dabae8851064d4760af",
          "productName": "hand bag",
          "productPrice": "20",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
          "qty": 1
        }
      ],
      [
        {
          "_id": "626a5d5dae8851064d4760ac",
          "productName": "handmade bag and show",
          "productPrice": "5",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
          "qty": 2
        },
        {
          "_id": "626a5dabae8851064d4760af",
          "productName": "hand bag",
          "productPrice": "20",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
 "sellerId": "62470b37f2052b6a6463f9b3",
          "qty": 1
        }
      ],
      [
        {
          "_id": "626a5d5dae8851064d4760ac",
          "productName": "handmade bag and show",
          "productPrice": "5",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
"sellerId": "0062470b37f2052b6a6463f9b30",
          "qty": 2
        },
        {
          "_id": "626a5dabae8851064d4760af",
          "productName": "hand bag",
          "productPrice": "20",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b30",
          "qty": 1
        }
      ],
      [
        {
          "_id": "626a5d5dae8851064d4760ac",
          "productName": "handmade bag and show",
          "productPrice": "5",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
          "qty": 2
        },
        {
          "_id": "626a5dabae8851064d4760af",
          "productName": "hand bag",
          "productPrice": "20",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/homeImg3.jpg-1651137961920",
"sellerId": "0062470b37f2052b6a6463f9b45",
          "qty": 1
        }
      ],
      [
        {
          "_id": "626a5d5dae8851064d4760ac",
          "productName": "handmade bag and show",
          "productPrice": "5",
          "proFrontIMAGE": "https://omarjbtbucket.s3.us-west-1.amazonaws.com/productB.jpg-1651137883823",
 "sellerId": "62470b37f2052b6a64637788",
          "qty": 2
        },
        
      ],
      
      
    ],
    "__v": 0
  }
]

Here is my code sample from the node

router.get("/orders", async (req, res) => {
  const { sellerID } = req.query;
  try {
    const order = await SellerOrder.find({
      sellerId: sellerID ,
    });
    res.status(200).json(order);
  } catch (error) {
    res.status(500).json(error + "error fetching data");
  }
});
4
  • And the input you want to enter in order to get this expected result.... Commented May 4, 2022 at 14:18
  • thanks @turivishal, my expected result, should be list of object that has the same sellerId Commented May 4, 2022 at 14:20
  • thanks @nimrodserok, please i dont get that, can you please explain better Commented May 4, 2022 at 14:22
  • Only two items have sellerId... Commented May 4, 2022 at 14:24

1 Answer 1

3

For filter, you can try the nested $elemMatch operator because your data has nested array, Try aggregation operator to filter array in projection, you can add your required fields as well in projection,

  • $reduce to iterate loop of orderItem, set initial value as a blank array
  • $filter to iterate loop of first-level array to check sellerId and filter array
  • $concatArrays to concat current array with filtered array
const order = await SellerOrder.find({
  orderItem: {
    $elemMatch: {
      $elemMatch: {
        sellerId: sellerID
      }
    }
  }
},
{
  orderItem: {
    $reduce: {
      input: "$orderItem",
      initialValue: [],
      in: {
        $concatArrays: [
          {
            $filter: {
              input: "$$this",
              cond: {
                $eq: ["$$this.sellerId", sellerID]
              }
            }
          },
          "$$value"
        ]
      }
    }
  }
  // add other projection fields here
})

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.