Manish
Manish

Reputation: 21

How to solve insertOne is not a function problem

var MongoClient = require('mongodb').MongoClient;

var url = 'mongodb://localhost:27017/';

MongoClient.connect(url, {useNewUrlParser: true}, function(err, db){

  if(err) throw err;

  var dbo = db.db('mydb');

  var myObj = [{name: 'Company Inc', address: 'Highway 37'}];
  dbo.createCollection('Customers').insertone(myObj, function(err, res){

    if(err) throw err;
    console.log('1 document inserted!');
    db.close();
  });
});

My code is not working, the error is insertOne is not a function. My mongoDB shell version is 3.4.18 and npm installed mongodb module is 3.1.10.

Upvotes: 1

Views: 3721

Answers (2)

ANNAPOORNESWARI H P
ANNAPOORNESWARI H P

Reputation: 11

just check the case of letter o in your insertOne function ..

Upvotes: 1

ecg8
ecg8

Reputation: 1392

Try inserting your record without creating the collection, as insertOne will automatically create the collection if it does not exist.

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/';
MongoClient.connect(url, {useNewUrlParser: true}, function(err, db){
  if(err) throw err;
  var dbo = db.db('mydb');
  var myObj = [{name: 'Company Inc', address: 'Highway 37'}];
  var collection = db.collection('Customers');
  collection.insertOne(myObj, function(err, res){
    if(err) throw err;
    console.log('1 document inserted!');
    db.close();
  });
});

https://docs.mongodb.com/manual/reference/method/db.collection.insertOne/

Upvotes: 1

Related Questions