db.students.batchInsert is not a function :@(shell):1:1

來源:互聯網
上載者:User

標籤:nbsp   odi   nts   owa   bat   AC   實現   error   cer   

按照《mongdb權威指南》當使用version 3.4.1版本的mongodb,其中使用batchInsert函數進行對students集合進行批量插入  

db.students.batchInsert([{"classid" : 1, "age" : 20, "name" : "kobe"},  {"classid" : 1, "age" : 23, "name" : "nash"}, {"classid" : 2, "age" : 18, "name" : "james"},  {"classid" : 2, "age" : 19, "name" : "wade"},  {"classid" : 2, "age" : 19, "name" : "bosh"},  {"classid" : 2, "age" : 25, "name" : "allen"},  {"classid" : 1, "age" : 19, "name" : "howard"},  {"classid" : 1, "age" : 22, "name" : "paul" },  {"classid" : 2, "age" : 24, "name" : "shane"}]);  

插入失敗,報如下錯誤:

2018-02-26T21:08:40.382+0800 E QUERY [main] TypeError: db.students.batchInsert is not a function :@(shell):1:1

原因:書中樣本batchInsert是按照version:2.4.0啟動並執行,到本版本已經廢棄該方法

解決方案:直接使用insert實現對students集合批量插入

db.students.insert([{"classid" : 1, "age" : 20, "name" : "kobe"},  {"classid" : 1, "age" : 23, "name" : "nash"}, {"classid" : 2, "age" : 18, "name" : "james"},  {"classid" : 2, "age" : 19, "name" : "wade"},  {"classid" : 2, "age" : 19, "name" : "bosh"},  {"classid" : 2, "age" : 25, "name" : "allen"},  {"classid" : 1, "age" : 19, "name" : "howard"},  {"classid" : 1, "age" : 22, "name" : "paul" },  {"classid" : 2, "age" : 24, "name" : "shane"}]);  db.students.find()

控制台輸出如下,插入成功

// Command #1:// Execution time: 0.0s// Result:BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 9,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ]})// Command #2:// db.students.find()// Execution time: 0.0s// Result:{ "_id" : ObjectId("5a940a3f379afc334959cacc"), "classid" : 1, "age" : 20, "name" : "kobe" }{ "_id" : ObjectId("5a940a3f379afc334959cacd"), "classid" : 1, "age" : 23, "name" : "nash" }{ "_id" : ObjectId("5a940a3f379afc334959cace"), "classid" : 2, "age" : 18, "name" : "james" }{ "_id" : ObjectId("5a940a3f379afc334959cacf"), "classid" : 2, "age" : 19, "name" : "wade" }{ "_id" : ObjectId("5a940a3f379afc334959cad0"), "classid" : 2, "age" : 19, "name" : "bosh" }{ "_id" : ObjectId("5a940a3f379afc334959cad1"), "classid" : 2, "age" : 25, "name" : "allen" }{ "_id" : ObjectId("5a940a3f379afc334959cad2"), "classid" : 1, "age" : 19, "name" : "howard" }{ "_id" : ObjectId("5a940a3f379afc334959cad3"), "classid" : 1, "age" : 22, "name" : "paul" }{ "_id" : ObjectId("5a940a3f379afc334959cad4"), "classid" : 2, "age" : 24, "name" : "shane" }

db.students.batchInsert is not a function :@(shell):1:1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.