mongo shell script won't let me include “use

前端 未结 3 1604
失恋的感觉
失恋的感觉 2020-12-25 12:26

32-bit mongo 2.0.1 on a windows XP machine

//script filename: test.js  (one line shell script file to store a person)
db.cTest.save({Name: \"Fred\", Age:21})         


        
3条回答
  •  生来不讨喜
    2020-12-25 12:58

    In a mongo script you can use the db.getSiblingDB('new_db_name') to get a reference of a new database. So, it it not mandatory to give the database name in the command line. You can use the script.js:

    db = db.getSiblingDB('new_db_name');
    print(db);
    
    // the rest of your code for database "new_db_name"
    

    and the output of this script is (invoked with mongo script.js):

    MongoDB shell version: 2.2.2
    connecting to: test
    sag
    

提交回复
热议问题