第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 mongoDB 和 Schema 中保存數(shù)據(jù)如下

如何在 mongoDB 和 Schema 中保存數(shù)據(jù)如下

白衣非少年 2021-11-04 17:38:23
這是我使用 mongoose npm 包的架構(gòu)。var StatusSchema = new mongoose.Schema({    empName: {        projectName: { type: String },        clientName: { type: String },        statusLastWeek: { type: String },        statusThisweek: { type: String },        planNextWeek: { type: String }    }});這是我的 nodejs 代碼來更新數(shù)據(jù)    var Status = mongoose.model('Status', StatusSchema);    module.exports = Status;      Description: Want save data in MongoDB, data schema is like above mentioned,       save saving data is sored loke as bellow.        Inside Mongo DB :    { "_id" : ObjectId("5d92f4aba4695e2dd90ab438"), "__v" : 0 }    { "_id" : ObjectId("5d92f4b4a4695e2dd90ab439"), "__v" : 0 }MongoDB 中的預(yù)期集合:     Dave Smith {        projectName: BLE Mesh,        clientName: Tera,        statusLastWeek: BLE Scan,        statusThisweek: BLE List View,        planNextWeek:   Mqtt config    }在這里你可以看到我的 NodeJS 代碼: router.post ('/update', (req,res,next)=>{        userStatus = new wkStatus(req.body)         userStatus.save()        .then(status => {          res.redirect('/success');          console.log ("Status saved in DB")        })       .catch(err => console.log(err))      // return next;  });
查看完整描述

3 回答

?
慕哥9229398

TA貢獻1877條經(jīng)驗 獲得超6個贊

//You can use ODM like mongoose and define a schema with mongoose.Schema. You can just 

// see mongoose module document from npm. Use .save() for save an object in DB.


// Example : 


// schema as admin


const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

const Schema = mongoose.Schema;

const bcrypt = require('bcrypt-nodejs');

const sha256 = require('sha256')


const adminSchema = new Schema({

    fullName: { type: String, required: true },

    userName: { type: String },

    noc: { type: String, required: true },

    mobileNumber: { type: String, required: true },

    email: { type: String },

    chacommAddress: {

        contactPerson: { type: String },

        country:  { type: String },

        address:  { type: String },

        city:  { type: String },

        pinCode:  { type: String },

        state:  { type: String },

        stateCode:  { type: String },

    },

    address: {

        country: { type: String },

        city: { type: String },

        pinCode: { type: String },

        state: { type: String },

        stateCode: { type: String },

        address: { type: String },

        CIN: { type: String },

        GSTIN: { type: String }

    },

    password: { type: String, required: true },

    userType: { type: Number, required: true },

    createdAt: { type: Date, required: true },

    uploadFile: { type: String, required: true },

    bankdetails: {

        bankName: { type: String },

        accountNo: { type: String },

        ifscCode: { type: String },

        accountType: { type: String },

        accountName: { type: String },

        cancelledChequeCopy: { type: String }

    },

    isActive: { type: Boolean },

    invoiceString:{type:String},

    invoiceValue:{type:Number},

    accountantName :{type:String} ,

    accountantDesignation : {type:String},

    referredBy:{type:String}

});


adminSchema.methods.comparePassword = function (password) {

    let password_hash = sha256(password);

    return bcrypt.compareSync(password_hash, this.password);

}


adminSchema.pre('save', function (next) {

    if (!this.isModified('password'))

        return next();

    let password_hash = sha256(this.password);

    bcrypt.hash(password_hash, null, null, (err, hash) => {

        if (err)

            return next(err);

        this.password = hash;

        next();

    });

});


//export schema

// module.exports = mongoose.model('Admin', adminSchema)



// for save:


const admin = require('admin')


var obj= new admin({

// values as per model defined

})


obj.save()


查看完整回答
反對 回復(fù) 2021-11-04
?
慕雪6442864

TA貢獻1812條經(jīng)驗 獲得超5個贊

const wkStatus = new wkStatus({

      _id: new mongoose.Types.ObjectId(),

      projectName: req.body.projectName,

      clientName: req.body.clientName,

      statusThisweek: req.statusThisweek,

      statusLastWeek: req.statusLastWeek,

      planNextWeek: req.planNextWeek

})

Status

   .save()

   .then(result => {

        res.status(201).json({

            message: "Data Created Successfully",

        })

       console.log(result) // show the response

   })

  .catch(err => {

       res.status(500).json({error:err})

  })

試試這種方式希望它會起作用。如果需要更多可以給我留言


查看完整回答
反對 回復(fù) 2021-11-04
?
郎朗坤

TA貢獻1921條經(jīng)驗 獲得超9個贊

您嘗試創(chuàng)建的架構(gòu)本身是錯誤的。


empName: {

    projectName: { type: String },

    clientName: { type: String },

    statusLastWeek: { type: String },

    statusThisweek: { type: String },

    planNextWeek: { type: String }

}

上面的模式可以創(chuàng)建如下對象:“empName”不能是動態(tài)的。


empName: {

    projectName: BLE Mesh,

    clientName: Tera,

    statusLastWeek: BLE Scan,

    statusThisweek: BLE List View,

    planNextWeek:   Mqtt config

}

如果你想像你展示的那樣存儲,empName那么你應(yīng)該動態(tài)地存儲empName,Map 參見https://mongoosejs.com/docs/schematypes.html#maps


查看完整回答
反對 回復(fù) 2021-11-04
  • 3 回答
  • 0 關(guān)注
  • 216 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號