知识点

软件工具

相关文章

更多

最近更新

更多

MongoDB shell 操作数据库

2019-03-15 21:51|来源: 网路

直接连接上某个主机的数据库
[ hadoop@huangyineng mongodb]$ /data/soft/mongodb/bin/mongo 192.168.56.101:27017/test


使用--nodb参数,不连接任何数据库,然后使用new Mongo("192.168.56.101:27017")连接上主机,再使用getDB连接数据库

[ hadoop@huangyineng mongodb]$ /data/soft/mongodb/bin/mongo --nodb
MongoDB shell version: 3.2.3
> conn=new Mongo("192.168.56.101:27017")
connection to 192.168.56.101:27017
> db=conn.getDB("test")


java连接数据库

public class MongoTest {
    MongoDatabase mongoDatabase = null;
    MongoClient mongoClient = null;
    @Before
    public void before() {
        mongoClient = new MongoClient("localhost", 27017);
        mongoDatabase = mongoClient.getDatabase("mydb");
    }
    @After
    public void after(){
        mongoClient.close();
    }
}


查看帮助
> help
   db.help()                    help on db methods
   db.mycoll.help()             help on collection methods
   sh.help()                    sharding helpers
   rs.help()                    replica set helpers
   help admin                   administrative help
   help connect                 connecting to a db help
   help keys                    key shortcuts
   help misc                    misc things to know
   help mr                      mapreduce
   show dbs                     show database names
   show collections             show collections in current database
   show users                   show users in current database
   show profile                 show most recent system.profile entries with time >= 1ms
   show logs                    show the accessible logger names
   show log [name]              prints out the last segment of log in memory, 'global' is default
   use <db_name>                set current database
   db.foo.find()                list objects in collection foo
   db.foo.find( { a : 1 } )     list objects in foo where a == 1
   it                           result of the last line evaluated; use to further iterate
   DBQuery.shellBatchSize = x   set default number of items to display on shell
   exit                         quit the mongo shell


查看数据库操作帮助

> db.help()
DB methods:
   db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
   db.auth(username, password)
   db.cloneDatabase(fromhost)
......


相关问答

更多
  • 用过echo加管道符将命令传入mongo的命令行 echo "db.serverStatus().mem" | mongo admin -u$user -p$pw 如果是多条命令 mongo admin -u$user -p$pw < 评论0 3 0 加载更多
  • 您好. #!/bin/sh mongo WordPress --eval "show collections;db.posts.find().limit(10);" show collections db.posts.find().limit(10) 如果还有问题,可以继续追问,感谢。
  • while(1){ $dbh = DBI->connect ($connection_string, $userid, $username); $sth=$dbh->prepare(insert into table(....) values(....)); $sth->execute sleep (300); }
  • shell操作数据库[2022-03-05]

    mysql -hhostname -Pport -uusername -ppassword -e 相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作mysql的方法。 #!/bin/bash HOSTNAME="192.168.111.84" #数据库信息 PORT="3306" USERNAME="root" PASSWORD="" DBNAME="test_db_test" #数据库名称 TABLENAME="test_table_test" #数据库中表的名 ...
  • 用过echo加管道符将命令传入mongo的命令行 echo "db.serverStatus().mem" | mongo admin -u$user -p$pw 如果是多条命令 mongo admin -u$user -p$pw < 评论0 0 0 加载更多
  • 在MongoDB中,文档是对数据的抽象,它被使用在Client端和Server端的交互中。所有的Client端(各种语言的Driver)都会使用这种抽象,它的表现形式就是我们常说的BSON(Binary JSON )。 BSON是一个轻量级的二进制数据格式。 MongoDB能够使用BSON,并将BSON作为数据的存储存放在磁盘中。 当Client端要将写入文档,使用查询等等操作时,需要将文档编码为BSON格式,然后再发送给Server端。同样,Server端的返回结果也是编码为BSON格式再放回给Clien ...
  • 如果您尝试导入的数据格式合理(例如逗号分隔),并且数据库服务器具有合理的命令行实用程序,则这应该不成问题。 MySQL具有“mysqlimport”命令行工具,它将接受描述文件格式的各种参数: mysqlimport \ --fields-terminated-by=, \ --ignore-lines=1 \ --fields-optionally-enclosed-by='"' < datafile.txt 通过perl / sed / awk单行程序传递数据可以帮助获取正确的 ...
  • 在数据库为空之前,它不会显示。 要显示数据库,您可以使用 create collection命令以插入集合然后它将通过使用显示给您 show dbs It will not show until your database is empty. To have your database shown you can use create collection command to insert collection then it will be shown to you by using show dbs ...
  • 您必须使用要执行请求的collection的名称替换collection .Plus更改集合的名称,以这种方式请求它(使用 - separator)将无效。 db.edx.course.students.find({}); 看看mongodb文档 You gotta replace the collection by the name of the collection you want to perform the request on.Plus change the name of your coll ...
  • 是的,您将使用Last Error命令 ,但您需要设置fsync标志(和/或复制参数 ,具体取决于您对“safe”的定义): # force fsync > db.runCommand({getlasterror:1,fsync:true}) # wait for replication to one other server (w = 2) > db.runCommand( { getlasterror : 1 , w : 2 } ) 如果您正在进行多次写入,则可以在最后一次写入后请求fsync或复制 ...