These are chat archives for Automattic/mongoose

18th
Sep 2018
Faran Ali
@Faran52_twitter
Sep 18 2018 04:47
@lineus so what is transform property of document about?, if it is that simple and can we perform asynchronous code in transform property
Faran Ali
@Faran52_twitter
Sep 18 2018 05:38
@lineus and how can I get _mongooseOption in post hooks ?
Faran Ali
@Faran52_twitter
Sep 18 2018 05:53
@lineus for existing keys I can change values how can I add new keys to the document in post hook (Just for returning not saving)
ratnesh2581990
@ratnesh2581990
Sep 18 2018 06:44

Hi @lineus can you help me in solving this problem I have I want to populate Product Id in my order collection from my product collection but I am confuse that populate will work on array field or not ?? i am pasting my structure of order collection and product collection so please help me in getting solution. Here is the structure of my order collection and I want to populate _id inside cartobject array and _5b93848db79f8100048948c1_PARINDA_30 so how can i do it in this kind of structure

{
    "_id": {
        "$oid": "5b9d0fb87c0a8700040a9712"
    },
    "cartobject": [
        {
            "_5b93848db79f8100048948c1_PARINDA_30": {
                "_id": "5b93848db79f8100048948c1",
                "sku": "1006",
                "brandSerialNumber": "45",
                "quantity": 5,
                "productName": "DESI TOOR DALL (SAWA NO.)",
                "brand": "PARINDA",
                "pack": {
                    "packvalue": "30",
                    "totpack": "30",
                    "innerpack": "1",
                    "packcharges": "0"
                },
                "price": 5200,
                "brandPrice": "5200",
                "offerMaxKgLimit": 0,
                "qtyInKg": 150,
                "deliveredQty": 0,
                "balanceQty": 5,
                "offer": 0,
                "offerPrice": "5200",
                "staticOfferPrice": 5200,
                "offerId": null,
                "fullOffer": null,
                "discountOnQuantity": 0,
                "staticDiscountOnQuantity": 0,
                "transportCharge": 40,
                "transportRate": 8,
                "totalItemPrice": 7840
            }
        }
    ],
    "orderdate": {
        "$date": "2018-09-15T19:27:10.000Z"
    },
    "customername": "User One 1111",
    "brokername": "Test",
    "amount": 7880,
    "orderid": 1537019823579,
    "ordernote": "",
    "userid": {
        "$oid": "5b50375de1b8d4000485ffd6"
    },
    "transportcharge": 40,
    "ordermilisecond": 1537019830006,
    "customernumber": null,
    "brokernumber": null,
    "__v": 0
}

Here is the structure of my product collection

{
    "_id": {
        "$oid": "5b93848db79f8100048948c1"
    },
    "productbrand": [
        {
            "serialnumber": "45",
            "productbrand": "PARINDA",
            "brand_pack_variation": [
                {
                    "packvalue": "30",
                    "totpack": "30",
                    "innerpack": "1",
                    "packcharges": "0"
                }
            ]
        }
    ],
    "creationdate": {
        "$date": "2018-09-08T08:13:01.930Z"
    },
    "producttitle": "DESI TOOR DALL (SAWA NO.)",
    "productbody": "DESI SAWA NO.",
    "sku": "1006",
    "unit": "QTL",
    "datemiliseconds": 1536394381930,
    "regularprice": 5200,
    "brandprice": {
        "PARINDA": "5200"
    },
    "ratechangedate": "08/09/2018",
    "__v": 0,
    "brandpricediff": {
        "PARINDA": "0"
    }
}
Kev
@lineus
Sep 18 2018 09:48
@ratnesh2581990 yeah, population will work on arrays, as long as mongoose knows which collection to populate from. This is determined either by the ref property in the virtual or orderSchema.cartObject._idpath or by adding the model property to the population object that gets passed into populate.
Kev
@lineus
Sep 18 2018 10:24
here's an example gist @ratnesh2581990
@Faran52_twitter are you referring to the transform property of toObject within documents?
Kev
@lineus
Sep 18 2018 10:37

@Faran52_twitter with regard to:

and how can I get _mongooseOption in post hooks ?

what specific property are you looking for in _mongooseOptions?

Kev
@lineus
Sep 18 2018 13:23

@Faran52_twitter with regard to:

for existing keys I can change values how can I add new keys to the document in post hook (Just for returning not saving)

you can:
1) define a virtual getter/setter on the schema for each property you want to add to the docs after retrieving them.
2) add the lean option to your query so that POJOs are returned instead of mongoose docs ( as long as you don't intend on using any mongoose methods on them )
3) you can add paths in the transform function of doc.toObject() but this function must be synchronous. ( the path will only be added to doc.toObject() )
here's an example

ratnesh2581990
@ratnesh2581990
Sep 18 2018 13:41
@lineus sorry but with your example link i am not able to understand in what way i can go ahead with structure as structure you have used in your is different then i have so can you please take my structure and provide me the example. it is better for me
Kev
@lineus
Sep 18 2018 14:21
@ratnesh2581990 can you share your schema instead of a doc? that will help me make a more relevant example.