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;
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
|
show 3 more comments
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
You should keep thatquantity
field inside theitems
collection.
– Ashh
Mar 28 at 17:55
No I am saying that you should keep it inside theitems
collection not with array of objects as you did hereitems: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]
– Ashh
Mar 29 at 7:10
Even if I add it like thisitems: [ 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 youritems
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
|
show 3 more comments
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
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
node.js mongodb mongoose pivot schema
asked Mar 28 at 13:36
shAkurshAkur
5911 bronze badges
5911 bronze badges
You should keep thatquantity
field inside theitems
collection.
– Ashh
Mar 28 at 17:55
No I am saying that you should keep it inside theitems
collection not with array of objects as you did hereitems: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]
– Ashh
Mar 29 at 7:10
Even if I add it like thisitems: [ 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 youritems
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
|
show 3 more comments
You should keep thatquantity
field inside theitems
collection.
– Ashh
Mar 28 at 17:55
No I am saying that you should keep it inside theitems
collection not with array of objects as you did hereitems: [ item: type: mongoose.Schema.Types.ObjectId, ref: 'Item' , quantity: 'String' ]
– Ashh
Mar 29 at 7:10
Even if I add it like thisitems: [ 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 youritems
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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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)
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment
|
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)
add a comment
|
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)
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)
answered Mar 29 at 8:30
shAkurshAkur
5911 bronze badges
5911 bronze badges
add a comment
|
add a comment
|
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You should keep that
quantity
field inside theitems
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 hereitems: [ 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