http://topmanopensource.iteye.com/blog/1278812### connection notation: [IP Address: port number]
MONGO 192.168.1.161:27017;
Show dbs;db;--shows the database being used
Use Asdfwerwe_test;
Db.tab_data_result.find ();
Db.tab_data_period.find ();
# # import, import data format is very rigorous
mongoimport-h 192.168.1.161:27017-d asdfswe_test-c tab_data_result d:\data.txt
Db.things.remove ({"X": "12"});
Conditions
Db.collection.find ({"field": {$gt: Value}});
Db.collection.find ({"field": {$lt: Value}});
Db.collection.find ({"field": {$gte: Value}});
Db.collection.find ({"field": {$lte: Value}});
Range segment
Db.collection.find ({"field": {$gt: value1, $lt: value2}});
$all Match all:
Db.users.find ({age:{$all: [6,7,8]}});
$exists determine if a field exists
Db.users.find ({age:{$exists: true});
does not exist
Db.users.find ({age:{$exists: false}});
Processing of NULL
Db.things.find ({y:null});
Querying for objects that contain null
Db.things.find ({age:{"$in": [null], "$exists": True}}); Basic operations
Db. AddUser (Username,password)//Add Users
Db.auth (Usrename,password)//Setting Up Database connection validation
//Example MONGO 192.168.0.178use admin db.auth ("Mongosa", "abc123d");//Authorized Login Use basdfsd_test;
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 Database Fromdb---Source database name, TODB---target database name, fromhost---source database server address
Db.createcollection (' name ', {size:3333,capped:333,max:88888})//Create a dataset equivalent to a table db.currentop ()//Cancel the current operation of the current library
Db.dropdatabase ()//delete current database
Db.eval (Func,args)//run code server-side
Db.getcollection (CNAME)//Get a data set, same usage: db[' cname ') or
Db.getcollenctionnames ()//Get a list of all data collection names
Db.getlasterror ()//Return the last Error prompt message
Db.getlasterrorobj ()//returns the last Error object
Db.getmongo ()//Get 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 operation database
Db.getpreverror ()//Return previous Error Object
Db.getprofilinglevel ()
Db.getreplicationinfo ()//Get 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 library
Db.printcollectionstats ()//returns the current library's dataset status
Db.printreplicationinfo ()
Db.printslavereplicationinfo ()
Db.printshardingstatus ()//returns whether the current database is a shared database
Db.removeuser (username)//delete user
Db.repairdatabase ()//repair 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 current service program
Db.version ()//Return version information for the current program
Data set (table) operations
Db.name.drop ();//Delete a collection
Db.test.find ({id:10})//Returns the DataSet id=10 the test dataset
Db.test.find ({id:10}). Count ()//Returns the total number of data id=10 the test dataset
Db.test.find ({id:10}). Limit (2)//Return Data set for test dataset id=10 starting at the second data set
Db.test.find ({id:10}). Skip (8)//Return data set from 0 to eighth of the DataSet id=10 the test dataset
Db.test.find ({id:10}). Limit (2). Skip (8)//Return data from the test dataset id=1= data set from second to eighth
Db.test.find ({id:10}). Sort ()//Returns the sorted data set id=10 the test dataset
Db.test.findOne ([query])//Return a qualifying piece of data
Db.test.getDB ()//returns the name of the database to which this dataset belongs
Db.test.getIndexes ()//Returns the index information of some data sets
Db.test.group ({key:...,initial:...,reduce: ... [, cond: ...]})
Db.test.mapReduce (mayfunction,reducefunction,<optional params>)
Db.test.remove (query)//delete a single piece of data in the data set
Db.test.renameCollection (NewName)//rename some data set names
Db.test.save (obj)//Insert a piece of data into the data set
Db.test.stats ()//Returns the state of this dataset
Db.test.storageSize ()//returns the storage size of this dataset
Db.test.totalIndexSize ()//returns the index file size of this dataset
Db.test.totalSize ()//Returns the total size of some datasets
Db.test.update (Query,object[,upsert_bool])//update one piece of data in this data set
Db.test.validate ()//verify this data set
Db.test.getShardVersion ()//Return data set share version number
Comparison between MongoDB syntax and existing relational database SQL syntax
Db.test.find ({' name ': ' Foobar '})
SELECT * FROM Test WHERE name= ' Foobar '
Db.test.find ()
SELECT * FROM Test
Db.test.find ({' data_id ': ten}). Count ()
SELECT COUNT (*) from Test WHERE data_id=10
Db.test.find (). Skip. Limit (20)
SELECT * FROM Test LIMIT 10,20
Db.test.find ({' data_id ': {$in: [25,35,45]}})
SELECT * FROM Test WHERE data_id in (25,35,45)
Db.test.find (). Sort ({' data_id ': -1})//SELECT * from Test ORDER by data_id DESCdb.test.find (). Sort ({' data_id ': 1})
SELECT * FROM Test ORDER by data_id ASC
Db.test.distinct (' name ', {' data_id ': {$lt: 20}})
SELECT DISTINCT (name) from Test WHERE data_id<20
Db.test.group ({key:{' name ': ' true},cond:{' name ': ' foo '},reduce:function (obj,prev) {prev.msum+=obj.marks;},initial: {msum:0}})
SELECT Name,sum (marks) from test GROUP by name
Db.test.find (' this.data_id<20 ', {name:1})
SELECT name from Test WHERE data_id<20
Db.test.insert ({' name ': ' Foobar ', ' Age ': 25})
INSERT into Test (' name ', ' age ') VALUES (' Foobar ', 25)
Db.test.remove ({})
DELETE * FROM Test
Db.test.remove ({' Age ': 20})
DELETE Test WHERE age=20
Db.test.remove ({' age ': {$lt: 20}})
Selete Test WHERE age<20
Db.test.remove ({' age ': {$lte: 20}})
DELETE Test WHERE age<=20
Db.test.remove ({' age ': {$gt: 20}})
DELETE Test WHERE age>20
Db.test.remove ({' age ': {$gte: 20}})
DELETE Test WHERE age>=20
Db.test.remove ({' age ': {$ne: 20}})
DELETE Test WHERE age!=20
Db.test.update ({' name ': ' Foobar '},{$set: {' age ': 36}})
UPDATE test SET age=36 WHERE name= ' Foobar '
Db.test.update ({' name ': ' Foobar '},{$inc: {' Age ': 3}})
UPDATE test SET age=age+3 WHERE name= ' Foobar '
MongoDB Usage Summary
1. Start the MongoDB service using:
Mongod--help
Local Service to start MongoDB (data directory and bin same level)
Mongod--dbpath. /data
Starting from the parameter file
Mongod-f/etc/mongodb.cnf
Background start mode:
Mongod--dbpath=. /data--logpath=. /logs--fork
2. Login to the MongoDB database
MONGO--help
Log on to the local database
Mongo
Log in to the remote database:
MONGO 123.123.101.41:27017/mash5
3. Restore the local dump file
View Help:
Mongorestore--help
Import a local dump file
Mongorestore C:\mongodb\bin\dump_bak_11241739\mash5
Import multiple databases
Mongorestore--directoryperdb C:\mongodb\bin\dump_bak_11241739
4. Backing Up the database
View Help
Mongodump--help
Generate a dump directory in the mongodump command directory to store export files (export multiple databases)
Mongodump
Export a database to a directory (single database)
Mongodump--db Mash5-o C:\temp
Remote Export
mongodump-h localhost--port 27017-o c:\temp
5. View the database performance of MongoDB
View Help
Mongostat--help
See all performance-related scenarios
Mongostat
6. View MONGO related System Information
See MONGOs's Help
MONGOs--help
View the version of MONGO
MONGOs--version
Modify some of the MONGO database related parameter information can be used MONGOs
Gridfs related file information in 7.mongo database
8.mongoexport Export
Export data information for things tables in the test database
mongoexport-d test-c Things-o Things.dat
Parameter description
-d indicates which library to use, in this case "My_mongodb"
-C indicates the table to be exported, in this case "user"
-O indicates the name of the file to be exported, in this case "User.dat"
From the above you can see how the export is using the JSON style
The X, y fields in the things table in the export test database are exported in CSV format
mongoexport-d test-c things-f x, y--csv-o things.data
The X, y fields in the things table in the output test database are displayed in a JSON array format
mongoexport-d test-c things-f x, y--jsonarray
9.mongoimport Import
Import the test database in JSON format things table
mongoimport-d test-c things--type json--drop--file things.data
Import the CVS database
./mongoimport-d my_mongodb-c User--type csv--headerline--file
User_csv.dat
Parameter description
-type indicates the file format to import
-headerline The first row is not imported because the first row is a column name
-file indicates the file path to import
MONGO Common commands:
Ask for help
Help
View all databases
Show DBS
View all tables in a database
Show collection
View User
Show Users
Show profile;
View the name of the host
hostname ();
View current directory
PWD ();
To view database-related help:
Db.help ();
View MONGO database current database server status
Db.serverstatus ();
To view the current database name:
Db.getname ();
View current version
Db.version ();
To view a database table
Db.getcollectionnames ();
To see if a table exists for a database
Db.getcollection ("Feed");
Whether the primary database
Db.ismaster ();
The situation of the database
Db.stats ();
Close the database
Use admin
Db.shutdownserver ();
Gets the currently connected object
Db.getmongo ()
Delete MONGO Database
Db.dropdatabase ();
Create a MONGO Table object
Db.createcollection ("TBS");
Table object to delete data
Db.mash5.drop ();
Show actions for the current database
Db.currentop ();
To view all database commands:
Db.listcommands ();
To view database-related commands:
Db.help ();
Common MongoDB SQL statements:
Paging Query
Db. Feed.find (). Skip. Limit (10000);
Single condition Query
Db. Task.find ({"tag": "Personal"}). Limit (50);
Multiple criteria Query
Db. Task.find ({"tag": "Personal", "_id": ObjectId ("4e169d85cc4370e29bc6c72e")}). Limit (.). explain ();
Query based on structure type
Db. Task.find ({"Bo. Fields.label ":" Private Messages "}). Limit (+). Explain ();
Db. Task.find ({"Status": "1"}). Count ();
Db. Task.findone ({"Status": "1"});
JS Features:
for (Var i=0;i<20;i++) {
Db. Task.save ({x:i*4,y:i*i});
}
var cursor=db. Task.find ();
while (Cursor.hasnext ()) {
Printjson (Cursor.next ());
}
Db. Task.find (). ForEach (Printjson);
var cursor=db.things.find ();
Printjson (Cursor[4]);
var arr=db.things.find (). ToArray ();
ARR[5];
Printjson (Db.things.findOne ({name: "MongoDB"}));
Db.things.update ({name: "MongoDB"},{$set: {name: "mongo_db"}})
Mngodb Common syntax