Pivot table for Mongoose Schema ObjectIDHow do I update/upsert a document in Mongoose?MySQL pivot tableWhat is the “__v” field in Mongooseadd created_at and updated_at fields to mongoose schemasSub documents inheritance in mongoose and mongoose-schema-extendUsing UUIDs in mongoose for ObjectID referencesUpdate Issues with Put Request, JQuery, MongoDB and MongooseMongoose sort by populated fieldDefining a map with ObjectId key and array of strings as value in mongoose schemaHow to manually set createdAt timestamp in mongoDB using mongoose?

Can Brexit be undone in an emergency?

How to give my students a straightedge instead of a ruler

Is it safe to unplug a blinking USB drive after 'safely' ejecting it?

Madrid to London w/ Expired 90/180 days stay as US citizen

Could the Orion project pusher plate model be used for asteroid deflection?

Why are there no programmes / playbills for movies?

Did slaves have slaves?

Are lay articles good enough to be the main source of information for PhD research?

(How long) Should I indulge my new co-workers?

How do rulers get rich from war?

With a 500GB SSD and a 250GB SSD is it possible to mirror a 250GB partition on the 500GB with the 250GB SSD using ZFS?

What is the maximum viable speed for a projectile within earth's atmosphere?

Secondary characters in character-study fiction

Plot irregular circle in latex

Should I inform my future product owner that there is a good chance that a team member will leave the company soon?

MySQL - How to check for a value in all columns

Is there a theorem in Real analysis similar to Cauchy's theorem in Complex analysis?

Can I separate garlic into cloves for storage?

Compare FEM mesh with the mesh created within Mathematica

Applications of mathematics in clinical setting

Is Zack Morris's 'time stop' ability in "Saved By the Bell" a supernatural ability?

Why is belonging not transitive?

What was the earliest microcomputer Logo language implementation?

What's the word for a student who doesn't register but goes to a class anyway?



Pivot table for Mongoose Schema ObjectID


How do I update/upsert a document in Mongoose?MySQL pivot tableWhat is the “__v” field in Mongooseadd created_at and updated_at fields to mongoose schemasSub documents inheritance in mongoose and mongoose-schema-extendUsing UUIDs in mongoose for ObjectID referencesUpdate Issues with Put Request, JQuery, MongoDB and MongooseMongoose sort by populated fieldDefining a map with ObjectId key and array of strings as value in mongoose schemaHow to manually set createdAt timestamp in mongoDB using mongoose?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have the following List schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
],
);


Everything works fine when I populate it with items. However, I want to have an additional column for each item in the list so I changed the schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
item:
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
,
quantity: 'String'
],
);


The populate method doesn't work with the above approach, unfortunately.



For a relational database I'd use a pivot table to save list id, item id and quantity but I really don't know how MongoDB treats such cases. Any suggestions are welcomed.










share|improve this question
























  • You should keep that quantity field inside the items collection.

    – Ashh
    Mar 28 at 17:55











  • No I am saying that you should keep it inside the items collection not with array of objects as you did here items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]

    – Ashh
    Mar 29 at 7:10











  • Even if I add it like this items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item', quantity: 'String' , ], it still doesn't add the quantity value to the database

    – shAkur
    Mar 29 at 7:22











  • Could you show your items collection

    – Ashh
    Mar 29 at 7:26












  • const ItemSchema = mongoose.Schema( name: type: String, required: true, min: 1 , created_at: type: Date, default: Date.now , );

    – shAkur
    Mar 29 at 7:31

















0















I have the following List schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
],
);


Everything works fine when I populate it with items. However, I want to have an additional column for each item in the list so I changed the schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
item:
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
,
quantity: 'String'
],
);


The populate method doesn't work with the above approach, unfortunately.



For a relational database I'd use a pivot table to save list id, item id and quantity but I really don't know how MongoDB treats such cases. Any suggestions are welcomed.










share|improve this question
























  • You should keep that quantity field inside the items collection.

    – Ashh
    Mar 28 at 17:55











  • No I am saying that you should keep it inside the items collection not with array of objects as you did here items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]

    – Ashh
    Mar 29 at 7:10











  • Even if I add it like this items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item', quantity: 'String' , ], it still doesn't add the quantity value to the database

    – shAkur
    Mar 29 at 7:22











  • Could you show your items collection

    – Ashh
    Mar 29 at 7:26












  • const ItemSchema = mongoose.Schema( name: type: String, required: true, min: 1 , created_at: type: Date, default: Date.now , );

    – shAkur
    Mar 29 at 7:31













0












0








0








I have the following List schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
],
);


Everything works fine when I populate it with items. However, I want to have an additional column for each item in the list so I changed the schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
item:
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
,
quantity: 'String'
],
);


The populate method doesn't work with the above approach, unfortunately.



For a relational database I'd use a pivot table to save list id, item id and quantity but I really don't know how MongoDB treats such cases. Any suggestions are welcomed.










share|improve this question














I have the following List schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
],
);


Everything works fine when I populate it with items. However, I want to have an additional column for each item in the list so I changed the schema:



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
item:
type: mongoose.Schema.Types.ObjectId,
ref: 'Item'
,
quantity: 'String'
],
);


The populate method doesn't work with the above approach, unfortunately.



For a relational database I'd use a pivot table to save list id, item id and quantity but I really don't know how MongoDB treats such cases. Any suggestions are welcomed.







node.js mongodb mongoose pivot schema






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 13:36









shAkurshAkur

5911 bronze badges




5911 bronze badges















  • You should keep that quantity field inside the items collection.

    – Ashh
    Mar 28 at 17:55











  • No I am saying that you should keep it inside the items collection not with array of objects as you did here items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]

    – Ashh
    Mar 29 at 7:10











  • Even if I add it like this items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item', quantity: 'String' , ], it still doesn't add the quantity value to the database

    – shAkur
    Mar 29 at 7:22











  • Could you show your items collection

    – Ashh
    Mar 29 at 7:26












  • const ItemSchema = mongoose.Schema( name: type: String, required: true, min: 1 , created_at: type: Date, default: Date.now , );

    – shAkur
    Mar 29 at 7:31

















  • You should keep that quantity field inside the items collection.

    – Ashh
    Mar 28 at 17:55











  • No I am saying that you should keep it inside the items collection not with array of objects as you did here items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]

    – Ashh
    Mar 29 at 7:10











  • Even if I add it like this items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item', quantity: 'String' , ], it still doesn't add the quantity value to the database

    – shAkur
    Mar 29 at 7:22











  • Could you show your items collection

    – Ashh
    Mar 29 at 7:26












  • const ItemSchema = mongoose.Schema( name: type: String, required: true, min: 1 , created_at: type: Date, default: Date.now , );

    – shAkur
    Mar 29 at 7:31
















You should keep that quantity field inside the items collection.

– Ashh
Mar 28 at 17:55





You should keep that quantity field inside the items collection.

– Ashh
Mar 28 at 17:55













No I am saying that you should keep it inside the items collection not with array of objects as you did here items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]

– Ashh
Mar 29 at 7:10





No I am saying that you should keep it inside the items collection not with array of objects as you did here items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]

– Ashh
Mar 29 at 7:10













Even if I add it like this items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item', quantity: 'String' , ], it still doesn't add the quantity value to the database

– shAkur
Mar 29 at 7:22





Even if I add it like this items: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item', quantity: 'String' , ], it still doesn't add the quantity value to the database

– shAkur
Mar 29 at 7:22













Could you show your items collection

– Ashh
Mar 29 at 7:26






Could you show your items collection

– Ashh
Mar 29 at 7:26














const ItemSchema = mongoose.Schema( name: type: String, required: true, min: 1 , created_at: type: Date, default: Date.now , );

– shAkur
Mar 29 at 7:31





const ItemSchema = mongoose.Schema( name: type: String, required: true, min: 1 , created_at: type: Date, default: Date.now , );

– shAkur
Mar 29 at 7:31












1 Answer
1






active

oldest

votes


















0
















I figured it out.



const ListSchema = mongoose.Schema(
title: type: String, required: true, max: 100 ,
items: [
type: mongoose.Schema.Types.Mixed, ref: 'Item', quantity: 'String'
],
);


Basically I've changed the item's schema type from mongoose.Schema.Types.ObjectId to mongoose.Schema.Types.Mixed.



This way when you .populate('items._id') you'll get everything from items document AND your additional column (quantity)






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399000%2fpivot-table-for-mongoose-schema-objectid%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    I figured it out.



    const ListSchema = mongoose.Schema(
    title: type: String, required: true, max: 100 ,
    items: [
    type: mongoose.Schema.Types.Mixed, ref: 'Item', quantity: 'String'
    ],
    );


    Basically I've changed the item's schema type from mongoose.Schema.Types.ObjectId to mongoose.Schema.Types.Mixed.



    This way when you .populate('items._id') you'll get everything from items document AND your additional column (quantity)






    share|improve this answer





























      0
















      I figured it out.



      const ListSchema = mongoose.Schema(
      title: type: String, required: true, max: 100 ,
      items: [
      type: mongoose.Schema.Types.Mixed, ref: 'Item', quantity: 'String'
      ],
      );


      Basically I've changed the item's schema type from mongoose.Schema.Types.ObjectId to mongoose.Schema.Types.Mixed.



      This way when you .populate('items._id') you'll get everything from items document AND your additional column (quantity)






      share|improve this answer



























        0














        0










        0









        I figured it out.



        const ListSchema = mongoose.Schema(
        title: type: String, required: true, max: 100 ,
        items: [
        type: mongoose.Schema.Types.Mixed, ref: 'Item', quantity: 'String'
        ],
        );


        Basically I've changed the item's schema type from mongoose.Schema.Types.ObjectId to mongoose.Schema.Types.Mixed.



        This way when you .populate('items._id') you'll get everything from items document AND your additional column (quantity)






        share|improve this answer













        I figured it out.



        const ListSchema = mongoose.Schema(
        title: type: String, required: true, max: 100 ,
        items: [
        type: mongoose.Schema.Types.Mixed, ref: 'Item', quantity: 'String'
        ],
        );


        Basically I've changed the item's schema type from mongoose.Schema.Types.ObjectId to mongoose.Schema.Types.Mixed.



        This way when you .populate('items._id') you'll get everything from items document AND your additional column (quantity)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 29 at 8:30









        shAkurshAkur

        5911 bronze badges




        5911 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399000%2fpivot-table-for-mongoose-schema-objectid%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript