MongoDB usage notes
1. Installation Package http://www.mongodb.org/downloads
Ii. server use
LINUX version:
① Use tar-zxvfmongodb-linux-x86_64-1.2.4.tgz to decompress the installation package
② Mkdir-p/data/db create directory
③ Cd/data/db go to the db directory and create mkdir m_data m_log
④ Go to the bin directory of the mongodb decompression directory and use
./Mongod-fork -- dbpath =./data/db/m_data/-- logpath =./m_log.log -- logappend -- port = 27017 & start the server.
Windows version:
Run the following command to start the server:
Mongod -- bind_ip 127.0.0.1 -- logpath d: \ data \ logs -- logappend -- dbpath d: \ data \ db -- directoryperdb-install
[Note: Install mongodb as a service, bind the service to IP127.0.0.1, set the log file to d: \ data \ logs, and add method records. The data directory is d: \ data \ db. And each database will be stored in a separate directory (-- directoryperdb )]
Iii. Client usage:
LINUX version:
Go to the bin directory of MongoDB and use./mongo to the command line to perform routine operations such as adding, deleting, modifying, and querying.
Windows version:
Enter the bindirectory of mongodband use the cmd.exe -- host IP -- port to connect to the MongoDB server.
4. Stop MongDB
LINUX version:
If you can directly press CTRL + C in the shell window, if it is used as a background service, you can first use ps-ef | grep pid d to query the server's pid, then use kill-9 pid. The Pid here is the queried pid.
Windows version:
If you press CTRL + C in the Command window and use it as a system service, you need to stop the service in service management or terminate the process in window manager.
5. web Version Statistics Service Status
Use http: // IP (Server IP): Port
Vi. Syntax
1. Basic operations
Db. AddUser (username, password) Add User
Db. auth (usrename, password) sets database connection Verification
Db. cloneDataBase (fromhost) clone a database from the target server
Db. commandHelp (name) returns the help for the command
Db. copyDatabase (fromdb, todb, fromhost) copy the database fromdb --- source database name, todb --- target database name, fromhost --- source database server address
Db. createCollection (name, {size: 3333, capped: 333, max: 88888}) to create a dataset, equivalent to a table
Db. currentOp () cancels the current operation of the current database
Db. dropDataBase () Delete the current database
Db. eval (func, args) run code server-side
Db. getCollection (cname) gets a data set. In the same usage: db ['cname'] or
Db. getCollenctionNames () Get the name list of all data sets
Db. getLastError () returns the message indicating the last error.
Db. getLastErrorObj () returns the last error object
Db. getMongo () gets the connection object of the current server get the server
Db. getMondo (). setSlaveOk () allow this connection to read from then nonmaster membr of a replica pair
Db. getName () returns the name of the database to be operated on.
Db. getPrevError () returns the previous error object
Db. getProfilingLevel ()
Db. getReplicationInfo () to obtain duplicate data
Db. getSisterDB (name) get the db at the same server as this onew
Db. killOp () Stop (kill) the current operation in the current database
Db. printCollectionStats () returns the dataset status of the current database
Db. printReplicationInfo ()
Db. printSlaveReplicationInfo ()
Db. printShardingStatus () returns whether the current database is a shared database
Db. removeUser (username) delete a user
Db. repairDatabase () repairs the current database
Db. resetError ()
Db. runCommand (cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj: 1}
Db. setProfilingLevel (level) 0 = off, 1 = slow, 2 = all
Db. shutdownServer () Close the current service program
Db. version () returns the version information of the current program.