Home > OS >  Best way to return a nested object the matches a property requested
Best way to return a nested object the matches a property requested

Time:12-02

I'm trying to create a new object that only contains the a product array with the seller I req. I have an order object that has a product array. I'd like to return a specific seller. I tried:

const newOrders = orders.map((element) => {
        return {
          ...element,
          product: element.product.filter(
            (seller) => seller === req.currentUser!.id
          ),
        };
      });

does mongoose have a preferred method for doing what I bring to achieve? I've read through the find queries but none of the methods seem useful to this use case.

orders: [

{
userId: "638795ad742ef7a17e258693",
status: "pending",
shippingInfo: {
line1: "599 East Liberty Street",
line2: null,
city: "Toronto",
country: "CA",
postal_code: "M7K 8P3",
state: "MT"
},
product: [
{
title: "new image",
description: "a log description",
seller: "6369589f375b5196f62e3675",
__v: 1,
id: "63737e4b0adf387c5e863d33"
},
{
title: "Mekks",
description: "Ple",
seller: "6369589f375b5196f62e3675",
__v: 1,
id: "6376706808cf1adafd5af32f"
},
{
title: "Meeks Prodyuct",
description: "long description",
seller: "63868795a6196afbc3677cfe",
__v: 1,
id: "63868812a6196afbc3677d06"
}
],
version: 1,
id: "6388138170892249e01bdcba"
}
],

CodePudding user response:

In general, the approach you have used with the Array.map() and Array.filter() methods is a good way to create a new object with the desired properties. However, if you are using Mongoose and want to query the data from your database, you can use the Mongoose Model.find() method to filter the documents and return only the ones with the desired seller.

Here is an example of how you can use the Model.find() method to query the database and return only the documents with the desired seller:

const newOrders = await Order.find({
  "product.seller": req.currentUser!.id
});

This code uses the Model.find() method to query the Order collection and return only the documents that have a product array with a seller field that matches the id of the current user (req.currentUser!.id).

This approach has the advantage of querying the database directly, which can be more efficient than filtering the data in JavaScript after it has been retrieved from the database. However, it does require that you have a Mongoose model and a connection to a MongoDB database.

Overall, either approach is valid and can be used to achieve your goal, depending on your specific use case and requirements.

CodePudding user response:

Im sure this can be improved, doesn't feel that its the best way possible but it gets the result. Like the previous answer you have to find first the order the seller is in then find the products than filter the seller by the id. I'm using typescript and there's a bug https://github.com/microsoft/TypeScript/issues/50769 so you have to use the bracket notation.

  const orders = await Order.find({
    "product.seller": req.currentUser!.id,
   });

  const allOrders = orders[0].product;

  const sellerOrders = allOrders.filter((obj) => {
    return obj["seller"] === req.currentUser!.id;
   });
  • Related