Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: mongodb property insertmany is not a function

db.col.insertMany(
    [
        {
        "_id" : "tt0084726",
        "title" : "Star Trek II: The Wrath of Khan",
        "year" : 1982,
        "type" : "movie"
        },
        {
        "_id" : "tt0796366",
        "title" : "Star Trek",
        "year" : 2009,
        "type" : "movie"
        },
        {
        "_id" : "tt0084726",
        "title" : "Star Trek II: The Wrath of Khan",
        "year" : 1982,
        "type" : "movie"
        }
    ]
);

OS: LinuxMint 17.3 Rosa MongoDB: db version v2.6.12

I am participating in a course by the University of MongoDB. When I input the command as above into mongo shell, an Error occurs: TypeError: Property 'insertMany' of object test.col is not a function how to solve it? I have read the docs but still failed.

like image 776
muzikmoe Avatar asked Jun 02 '16 07:06

muzikmoe


People also ask

What is the difference between insert and insertMany in MongoDB?

With insertOne you can insert one document into the collection. insertMany accepts an array of documents and these are inserted. The insert (the method from older versions) takes a single document by default, and there is an option to insert multiple documents supplied as an array.

What does insertMany return?

The insertMany() method returns a document that contains: The acknowledged key sets to true if operation executed with a write concern or false if the write concern was disabled. An array of _id values of successfully inserted documents.

What is the command to insert many documents to the collections of a MongoDB database?

insertMany() can insert multiple documents into a collection. Pass an array of documents to the method. The following example inserts three new documents into the inventory collection. If the documents do not specify an _id field, MongoDB adds the _id field with an ObjectId value to each document.


2 Answers

I had a similar problem. You need to have version 3.2

> New in version 3.2.

You need to update your version of mongodb to use insertMany.

It's not so hard. I did it a couple weeks ago to use insertMany

like image 166
jack blank Avatar answered Oct 27 '22 11:10

jack blank


I am newbie in MongoDB , I got similar error in another scenario. I am adding that also an answer here, any future reader may find useful!

db.users.insertMany ([{name:"Ajith",status:"pending",age:25}, {name:"Kumar",status:"withheld",age:40}, {name:"Lal",status:"passed",age:34}, {name:"Tishil Joppen",status:"high",age:28}])

I forgot to include the square brackets , and got the same error!!

like image 30
Arun Avatar answered Oct 27 '22 13:10

Arun