2
import { MongoClient } from 'mongodb';
import assert from 'assert';
import config from './config';


console.log('Inside File');
MongoClient.connect(config.mongodbUri, (err, db) => {
  assert.equal(null, err);


 db.products.insertMany( [
      { item: "card", qty: 15 },
      { item: "envelope", qty: 20 },
      { item: "stamps" , qty: 30 }
   ] ); 
      db.close();

});

For the above code i get the error Cannot read property insertMany of undefined. But when i just enter the below

 db.products.insertMany( [
      { item: "card", qty: 15 },
      { item: "envelope", qty: 20 },
      { item: "stamps" , qty: 30 }
   ] ); 

in the console the products seems to get inserted in the db just fine. Can someone please let me know the cause of this issue?

user3804335
  • 483
  • 1
  • 5
  • 8

3 Answers3

4

Can try using db.collection('products').insertMany and callback function

MongoClient.connect(config.mongodbUri, (err, db) => {
  assert.equal(null, err);

 db.collection('products').insertMany( [
      { item: "card", qty: 15 },
      { item: "envelope", qty: 20 },
      { item: "stamps" , qty: 30 }
   ] , function(error, doc) {
        if(error) {
           console.log(error);
        } else {
           console.log('success');
        }
        db.close();
    }); 
});
Shaishab Roy
  • 16,335
  • 7
  • 50
  • 68
0

insertMany() is a mongoDB function, it will work if you try it in the console,
but in nodeJs you're dealing with Mongoose ( the mongoDb driver ) which doesn't have this function, to store items with mongoose, you need to do products.save() and it doesn't support bulk inserts ( you can only insert one at a time ) so you have to go through a loop :

var items = [{ item: "card", qty: 15 },
      { item: "envelope", qty: 20 },
      { item: "stamps" , qty: 30 }];

var Products = mongoose.model('Products'); // don't know what you have as a model

for ( var i in items) {
    Products.save(items[i]);
}

of course you will need to handle errors ..etc.

if you don't already have a model here's a snippet:

var Schema = mongoose.Schema; 
var products = new Schema({
    item : String
  , qty  : int
});
mongoose.model("Products", Products);
Taki
  • 17,320
  • 4
  • 26
  • 47
  • You can use Mongo driver in Node directly without using any ORM like Mongoose, here is an example: https://bezkoder.com/node-js-csv-mongodb-collection/ – Mostafa Darezereshki Jan 29 '21 at 02:25
0

Basically the function you're trying to use insertMany() is MongoDB function which can be directly used in the mongo console without any issues. But in case if you want to use it through your application that means using MongoClient library, you can do that too..

What you need to do is very simple, just use it the with the collection name you wanted to insert and passing the mandatory callback fn() as the other argument.

Syntax:

db.collection(<collectionName>).insertMany([{arglist1},{arglist2},
{arglistn..}],Callbackfn())

I Hope it'll be okay.

maazza
  • 7,016
  • 15
  • 63
  • 96
Ravi kumar
  • 170
  • 1
  • 2
  • 15