node.js - update schema in mongoose to add new property -


i trying update schema add new property field. hoping simple adding property schema updated field being accessible.

i have existing schema

  let userdrinkschema = new mongoose.schema({  new schema     creator : {          type: mongoose.schema.types.objectid,          ref: 'user'  // name of user file     },     caffeine: number,     mgfloz: number,     name: string,     size: number,     updated_at: {         type: date,         default: date.now()     } }); 

i need add id schema

const userschema = mongoose.schema({  const user = module.exports = mongoose.model('user', userschema); 

i not sure how add property each user. followed example dealing schema changes in mongoose

the schema now:

let drinkschema = new mongoose.schema({     user: {          type: mongoose.schema.types.objectid,          ref: 'user',         required: true,         default: null     },     caffeine: number,     mgfloz: number,     name: string,     size: number,     imageurl: string,     date: {         type: date,         default: date.now()     } }, {     collection: 'drinklist' }); 

unfortunately me user null well

{  "_id":"58bba13bb7afeaa3180aa929",  "caffeine":422,"mgfloz":218.7,  "name":"10 hour energy shot",  "size":1.93,  "url":"/caffeine-content/10-hour-eternal-shot",  "date":"2017-03-28t13:10:12.650z",  "user":null } 

is there way update user field user information? schema referencing set as

const selecteddrinks = require('./userdrinks').schema; const userschema = mongoose.schema({     name: {         type: string     },     email: {         type: string,         required: true     },     username: {         type: string,          required: true     },     password: {         type: string,         required: true     },     caffeine_list: [selecteddrinks] }) 

i can see mongodb adds id each user when created. trying access.

my backend route configured in express. postman can work becuase have user id. not sure how correct user id inside of route.

    router.post('/adddrink', (req, res, next) => {     let newdrink = new userdrinks({         creator: req.body.creator, // need id user object id()         caffeine: req.body.caffeine,         mgfloz: req.body.mgfloz,         name: req.body.name,         size: req.body.size,         updated_at: req.body.updated_at     });      newdrink.save( (err) => {         if(err) {             res.send(err);         } else {             user.findone({ _id: newdrink.creator},  (err, user) => {                 user.caffeine_list.addtoset(newdrink)                 user.save( function (err) {                     if(err) {                         console.log(err);                     }else {                         res.status(201).json(newdrink);                     }                 })             })          }      }) }); 

if i'm understanding right, you're trying replace creator field user field. accomplish you'll need 2 things:

  1. modify /adddrink route
  2. update existing documents in drinklist collection

1) when create newdrink, set user instead of creator:

let newdrink = new userdrinks({     user: req.body.creator,     caffeine: req.body.caffeine,     mgfloz: req.body.mgfloz,     name: req.body.name,     size: req.body.size,     updated_at: req.body.updated_at }); 

2) update userdrinks rename field:

userdrinks.update({}, { $rename: { "creator" : "user" } }, { multi: true }, callback) 

this find userdrinks documents , rename creator property user.

a quick way without having create new route one-time change create temporary function under express setup, run locally, , delete/comment code after doesn't run again. ex:

function renamecreatorfield() {     userdrinks.update({}, { $rename: { "creator" : "user" } }, { multi: true }, function(err, data) {         if (!err) {              //success          }     }) }  renamecreatorfield(); 

this run when server starts up. once runs once can delete because changes made in (1) ensure future userdrinks documents have correct field set.

of course, if you're looking quick fix , isn't live application can (1), clear out database, , start fresh correct documents. that's not way learn. :)


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -