Last Updated
Viewed 23 Times
        

im learning Nodejs and MongoDB, and I succesfully connected to database and saving to database is working, but if for exemple i add another item to the list, and run the script again to save to the database it duplicates the ones that already exists. I want to whenever I need to add a new item do the project, it only adds that specific item instead of adding everything again.

This is my seeder:

const mongoose = require("mongoose");
const Keys = require("../config/auth");

mongoose.set("useUnifiedTopology", true);
mongoose.set("useNewUrlParser", true);
mongoose.connect(Keys.MongoURI);

const products = [
  new Product({
    imagePath:
      "test",
    title: "test1",
    description: "test1",
    price: 20
  }),
  new Product({
    imagePath:
      "test2",
    title: "test2",
    description: "test2",
    price: 20
  })
];

let done = 0;
for (let i = 0; i < products.length; i++) {
  products[i].save(function(err, result) {
    done++;
    if (done === products.length) {
      exit();
    }
  });
}
function exit() {
  mongoose.disconnect();
}
console.log(products);

so if for example i add a new product here, it should check the database if like: title match on database then dont add to database, only the ones that the title are available.

Thanks!

const UserSchema = new mongoose.Schema({
    username: {type: String, unique:true},
    fullname: {type: String, unique:false,default:''},
    email: {type: String, unique:true},
    password: {type: String, unique:false, default: ''},
    userImage: {type: String, default:'default.png'},
    facebook: {type: String, default: ''},
    fbTokens: Array,
    google: {type: String, default:''}
}, {timestamps: true});

This is my User schema. If i delete the indexes that are created on email,password,username and fullname and restart my express app using mongoose, it recreates these db indices. How to prevent this from happening ?

If you have subdocument arrays, Mongoose automatically creates ids for each one. Example:

{
    _id: "mainId"
    subDocArray: [
      {
        _id: "unwantedId",
        field: "value"
      },
      {
        _id: "unwantedId",
        field: "value"
      }
    ]
}

Is there a way to tell Mongoose to not create ids for objects within an array?

So first of all im getting a post via a form that returns to me multiple lines of text

then i use the following to split it into multiple urls

var str = req.body.url;
var split = str.split('\n');

then i get this

[urlone,urltwo,urlthree]

my save function works fine for one document i just cant seem to submit all three at the same time to mongo via mongoose commands .

ive tried a few forEach loops . and got no help

I can really use some help here I'm really new to nodejs

Similar Question 4 (1 solutions) : Mongoose add multiple items to database

Similar Question 5 (1 solutions) : How to retrieve records from an mlab database

Similar Question 6 (1 solutions) : MongoDB how to choose data from database

Similar Question 7 (1 solutions) : How to get data from existing MongoDB database?

Similar Question 8 (1 solutions) : How to concatenate HREF to get id from database?

Similar Question 9 (1 solutions) : Cannot create items in database of keystone.js

cc