简体   繁体   中英

Push data to MongoDB with Mongoose

I try to build a favorite system on my website. When a user clicks a button, it select data and push it to the backend. This part is effective:

const dataPush = {
                            idSave: idSaveAttr,
                            urlSave: urlSaveAttr,
                            imgSave: imgSave,
                            marqueSave: marqueSave,
                            smSave: smSave,
                            typeSave: typeSave,
                            precisionSave: precisionSave,
                            yearsSave: yearsSave,
                            coteEuSave: coteEuSave,
                            coteUsdSave: coteUsdSave,
                            coteGbSave: coteGbSave,
                            coteChfSave: coteChfSave
                        }
                        console.log(dataPush)
                        //push to backend
                        $.post('/fr/save', dataPush);

On the backend, I have a User model:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require('passport-local-mongoose');

//Create Schema
const User = new Schema({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true,
        required: true
    }
    favorites: {
        type: [{
            idSave: {
                type: String
            },
            urlSave: {
                type: String
            },
            imgSave: {
                type: String
            },
            marqueSave: {
                type: String
            },
            smSave: {
                type: String
            },
            typeSave: {
                type: String
            },
            precisionSave: {
                type: String
            },
            yearsSave: {
                trype: String
            },
            coteEuSave: {
                type: String,
            },
            coteUsdSave: {
                type: String,
            },
            coteGbSave: {
                type: String,
            },
            coteChfSave: {
                type: String,
            }
        }]
    }
});

User.plugin(passportLocalMongoose);

mongoose.model('users', User);

But how to push all these elements to my Mongoose DB regarding my User model? My favorite backend:

//Load User Model
require('../../models/User');
const User = mongoose.model('users');

    router.post('/save', ensureAuthenticated, (req, res) => {
    const dataPush = req.body
    console.log(dataPush)
})
router.get('/save', csrfProtection, (req, res) => {
    res.send('yep')
})

And the output of the console.log:

[Object: null prototype] {
  idSave: '10000',
  urlSave: 'exmplae-of-so.com',
  imgSave: 'https://example.com/models.jpg',
  marqueSave: 'AC',
  smSave: '10 HP',
  typeSave: 'Cabriolet',
  precisionSave: '',
  yearsSave: '1913-1916',
  coteEuSave: '28 035 €',
  coteUsdSave: '$30,590',
  coteGbSave: '£24,911',
  coteChfSave: 'CHF 30’632'
}

I am not sure with your question but as per your comment i am suggesting, if favorite is new then add and if already available then update.

  • lets assume this is your initial variables,
let userId = mongoose.Types.ObjectId("123");
let data = req.body;
  • If user is adding new favorite:
if (!data.idSave) {
    User.update(
      { _id: userId }, 
      {
        $push: {
          "favorites.type": data
        }
      }
    )
}
  • if user is update existing favorite
else {
    User.update(
      {
        _id: userId, // optional 
        "favorites.type.idSave": data.idSave
      }, 
      {
        $set: {
          "favorites.type.$": data
        }
      }
    )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM