0

hi all i am studying node js for beginner making api from expres, i want to ask how to insert many data after cheking data exist and create data if data not exist? for example so i have data json in database

[{
   "_id":1,
   "product": t-shirt,
   "brand": polo,
   "price": ["3.335.000","4.000.000"],
   "detail": {
      "desc": "test1",
      "size": "L"
    }
 },
{
   "_id":2,
   "product": t-shirt,
   "brand": adidas,
   "price": ["2.335.000","3.000.000"],
   "detail": {
      "desc": "test2",
      "size": "L"
    }
 },{
   "_id":3,
   "product": t-shirt,
   "brand": puma,
   "price": ["5.000.000","9.000.000"],
   "detail": {
      "desc": "test3",
      "size": "XL"
    }
 }]

and i have data, for post/insert to db

[{
       "product": t-shirt,
       "brand": polo,
       "price": ["3.335.000","4.000.000"],
       "detail": {
          "desc": "test1",
          "size": "L"
        }
     },
    {
       "product": t-shirt,
       "brand": adidas,
       "price": ["2.335.000","3.000.000"],
       "detail": {
          "desc": "test2",
          "size": "L"
        }
     },{
       "product": t-shirt,
       "brand": puma,
       "price": ["5.000.000","9.000.000"],
       "detail": {
          "desc": "test3",
          "size": "XL"
        }
     },
    {
       "product": t-shirt,
       "brand": nike,
       "price": ["5.000.000","9.000.000"],
       "detail": {
          "desc": "test4",
          "size": "XL"
        }
     },
    {
       "product": t-shirt,
       "brand": aon,
       "price": ["5.000.000","9.000.000"],
       "detail": {
          "desc": "test5",
          "size": "XL"
        }
     }
    ]

UPDATE

THIS IS A CODE FROM server

var Gnew        = require('../models/global/Globals.Prod.js');
var fs          = require('fs');

module.exports = function(prouds) { 

//CREATE
prouds.post('/create', function(req, res, err){
  Gnew.find({}, function(err, proudcts){
    if (err){
      console.log(err)
    }else{
      // IF PRODUCTS === REQ.BODY THEN CREATE REQ.BODY NOT EXIST IN PRODUCT
      // HOW TO CREATE THIS?
    }
  })
return prouds;
};

i post data using postman, and after post i get all data from DB for check data, and i want if exist he just ignore the same data and i want post data brand nike and aon because brand of puma, adidas, and polo is exist and how to create codition ?

oke i've try condition like this, i just want to get data not exist in db, why the output its same as like input? what is wrong with this code? hhe

req.body.forEach(function(itm1){
    proudcts.forEach(function(itm){

      // if ((itm1.judul === itm.judul) === null || undefined){

      //       fs.writeFile('cek.json', JSON.stringify(req.body));  
      // }else{

      //       fs.writeFile('cek1.json', JSON.stringify(req.body));  

      // }
      if (!(itm1.judul === itm.judul)){

            fs.writeFile('cek.json', JSON.stringify(req.body));  
      }else{

            fs.writeFile('cek1.json', JSON.stringify(req.body));  

      }

    })
  })

thanks

Omlegron
  • 1
  • 1
  • 1
    Since you are just throwing a bunch of data around, I will just throw back a keyword, too. In MongoDB there are upserts. You can also perform bulk upserts, see: https://docs.mongodb.com/manual/reference/method/Bulk.find.upsert/ – kentor Nov 28 '17 at 19:55
  • @OopsD'oh oke will update – Omlegron Nov 28 '17 at 20:13
  • @kentor no, if i find with bulk or find with array, I'm afraid this will slow execute the mongose, because the data could exceed 20/30 – Omlegron Nov 28 '17 at 20:18
  • @Omlegron I don't understand what you expect? You said you want to upsert data (insert if data not exists) and above I just pointed out that you can use the bulk upsert method. Optionally you can use the findOneAndUpdate option with upsert option set to true. I use the bulk upsert method for a project where I perform more than 4000 upserts / second. – kentor Nov 28 '17 at 20:29
  • @kentor really... will it make slow execution in mongoose? – Omlegron Nov 28 '17 at 20:49
  • @kentor oke must i try – Omlegron Nov 28 '17 at 20:53
  • In Mongoose you must use the `collection` accessor for bulk upserts, see: https://stackoverflow.com/questions/25285232/bulk-upsert-in-mongodb-using-mongoose – kentor Nov 28 '17 at 21:00
  • @kentor ok i will try, thanks and if success i will post the code – Omlegron Nov 28 '17 at 21:06

0 Answers0