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.
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.
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.
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.
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
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With