zoukankan      html  css  js  c++  java
  • db.students.batchInsert is not a function :@(shell):1:1

    按照《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" }
    
    
  • 相关阅读:
    bootstrap 的页码显示问题-------------德州
    大神的---解决tomcat内存溢出问题----tomcat报错:This is very likely to create a memory leak问题解决
    如何设置tomcat,直接通过IP 访问
    如何把MyEclipse中的web项目导入到Eclipse中运行
    易捷框架之EChart 的使用
    打包jar文件并自动运行
    『PLSQL』在oracle表中怎样创建自增长字段?
    解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes
    Oracle 与 MySQL 批量添加
    SPR, subpixel rendering
  • 原文地址:https://www.cnblogs.com/zjfjava/p/8476123.html
Copyright © 2011-2022 走看看