0

Welcome ... I'm working with Node js and i have a problem Performance Problem Is this code better ... From my point of view the first one not the best because it may takes time for searching about specific item

cart: {
    items: [{
        productId: {
            type: Schema.Types.ObjectId,
            ref: 'Product'
        },
        offerId: {
            type: Schema.Types.ObjectId,
            ref: 'Offer'
        },
        quantity: {
            type: Number,
            required: true
        }
    }]
}

Or this

product_cart: {
    items: [{
        productId: {
            type: Schema.Types.ObjectId,
            ref: 'Product'
        },
        quantity: {
            type: Number,
            required: true
        }
    }]
},
offer_cart: {
    items: [{
        offerId: {
            type: Schema.Types.ObjectId,
            ref: 'Offer'
        },
        quantity: {
            type: Number,
            required: true
        }
    }]
},

Which of two codes are the best for performance ?

2
  • Well, your question is wrong. Nodejs does not have to do with this. All operation is done on MongoDB Side Commented Mar 9, 2020 at 4:25
  • Muhammad, you need to restructure your question. 1. Are you asking how to improve the performance of node js when you process this JSON in node js? 2. How to improve MongoDB schema for better performance? Commented Jun 11, 2021 at 15:55

2 Answers 2

2

you are using mongodb schema in node js, use this reference link: https://mongoosejs.com/docs/guide.html

this document to schema setup very easy

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

Comments

0

First of all your question is very generic. If the question is regarding on run-time preformance it depends on many factors not only the model schema. In any case you can use mongoose populate in order to access a specific field which your ObjectId is refering to. If this is your intention, You can read more aboute mongoose populate() here: https://mongoosejs.com/docs/populate.html

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.