mongo db simple operation

quote


Local operation database
1, specify the data file address of mongo in the first window, the data\db folder is to be newly created
D:\Tools\Mongo\mongodb-win32-i386-2.4.1\bin>mongod -dbpath "D:\Tools\Mongo\mongodb-win32-i386-2.4.1\data\db" 

2. Hit mongo in the second window, you can see if the connection is successful
D:\Tools\Mongo\mongodb-win32-i386-2.4.1\bin>mongo






http://blog.csdn.net/xxx9001/article/details/ 52196833


1. First install mongodb

1. Download address: http://www.mongodb.org/downloads

2. Unzip it to the directory you want to install, such as d:\mongodb

3. Create a folder d:\mongodb\data \db, d:\mongodb\data\log, used to install db and log files respectively, create a log file MongoDB.log in the log folder, namely d:\mongodb\data\log\MongoDB.log

4. Run cmd.exe enters the dos command interface and executes the following command

  > cd d:\mongodb\bin

  > d:\mongodb\bin>mongod -dbpath "d:\mongodb\data\db"



 If you see similar information, it means that the startup is successful. The default port monitored by MongoDB is 27017 and that of MySQL is 3306.

5. Test the connection Open a

 new cmd window, enter the bin directory of mongodb, enter mongo or mongo.exe, and the following information will appear. The test is passed. At this point, we have entered the test database. How to enter other databases will be described below.

 

 Enter exit or ctrl+C to exit.

6. When mongod.exe is closed, mongo.exe cannot connect to the database, so every time you want to use the mongodb database, you have to open the mongod.exe program, so it is more troublesome. At this time, we can install MongoDB as a windows service

 or Run cmd, enter the bin folder, and execute the following command

 > d:\mongodb\bin>mongod --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --install --serviceName "MongoDB"

 where MongoDB.log is the log file created, --serviceName "MongoDB" service name is MongoDB and

 then start the mongodb service

 > d:\mongodb\bin>NET START MongoDB

 

 opens the task manager, You can see that the process has started

7. Shut down the service and delete the process

 > d:\mongodb\bin>NET stop MongoDB (close the service)

 > d:\mongodb\bin>mongod --dbpath "d:\mongodb\data\db" --logpath "d:\mongodb\data\log\MongoDB.log" --remove --serviceName "MongoDB" (remove , Note that it is not --install)



Second, use mongodb

1. Common commands

show dbs to display the database list
use dbname to enter the dbname database, case-sensitive, it doesn't matter if there is no such database
show collections Displays the collections in the database, which is equivalent to table
2. Create & add

db.users.save({"name":"lecaf"}) Created a collection named users, and added a {"name":"lecaf"} data
db.users.insert( {"name":"ghost", "age":10}) Insert a new piece of data into the users collection. If there is no users collection, mongodb will automatically create
save() and insert(). There are also some differences: if The newly added data primary key already exists, insert() will do nothing and prompt an error, while save() will change the original content to new content.
Existing data: { _id : 1, " name " : " n1 "

save({ _id : 1, " name " : " n2 " }) will change n1 to n2 , which has the effect of update.
3. Delete

db.users.remove() Delete all data under users collection
db.users.remove({"name": "lecaf"}) Delete data with name=lecaf under users collection
db.users.drop() or db .runCommand({"drop","users"}) Delete the collection users
db.runCommand({"dropDatabase": 1}) Delete the current database
4. Find

db.users.find() Find all data in the users collection
db.users .findOne() Find the first data in the users collection
5. Modify

db.users.update({"name":"lecaf"}, {"age":10}) Modify the data of name=lecaf to age=10 , the first parameter is the search condition, the second parameter is the modification content, except for the primary key, other content will be replaced by the content of the second parameter, the primary key cannot be modified, as shown in Figure
 

3. Advanced application

1. Conditional search

db.collection. find({ "key" :

db.collection.find({ "key" : { $lt: value } }) key < value
db.collection.find({ "key" : { $gte: value } }) key >= value
db.collection.find ({ "key" : { $lte: value } }) key <= value
db.collection.find({ "key" : { $gt: value1 , $lt: value2 } }) value1 < key <value2
db.collection .find({ "key" : { $ne: value } }) key <> value
db.collection.find({ "key" : { $mod : [ 10 , 1 ] } }) Modulo operation, the condition is equivalent to key % 10 == 1 That is, db.collection.find({ "key" : { $nin: [ 1, 2, 3 ] } }) does not belong to db.collection.find({ "key" : { $nin: [ 1, 2, 3 ] } }) when the key is divided by 10 and the remainder is 1. The
condition is equivalent to whether the value of the key is not belongs to any one of [ 1, 2, 3 ]
db.collection.find({ "key" : { $in: [ 1, 2, 3 ] } }) belongs to, the condition is equivalent to key equal to [ 1, 2, 3 ] Any one of
db.collection.find({ "key" : { $size: 1 } }) $size number, size, the condition is equivalent to the number of key values ​​is 1 (key must be an array, a value cannot be regarded as an array with a number of 1)
db.collection.find({ "key" : { $exists : true|false } }) $exists field exists, true returns data with field key, false returns data without word key
db.collection.find({ "key": /^val.*val$/i }) Regular, similar to like; "i" ignores case, "m" supports multiple lines
db.collection.find({ $or : [{a : 1}, {b : 2} ] }) $or (Note: MongoDB 1.5.3 and later versions are available), the data that meets the condition a=1 or meets the condition b=2 will be queried
db.collection.find({ "key" : value , $or : [{ a : 1 } , { b : 2 }] }) Data that meets the condition key=value and meets either of the other two conditions
db.collection.find({ "key.subkey" :value }) matches the value in the embedded object, note: "key.subkey" must be quoted
db.collection.find({ "key": { $not : /^val.*val$/i } }) This is an operator used in combination with other query conditions and will not be used alone. The opposite set can be obtained by adding $not to the result set obtained by the above query conditions.
2. Sort

db.collection.find().sort({ "key1" : -1 , "key2" :


db.collection.find().limit(5) controls the number of returned results, if the parameter is 0, it is regarded as no constraint, limit() will not work
db.collection.find().skip(5) controls the returned results How much to skip, if the parameter is 0, it is regarded as no constraint, skip() will not work, or skip 0
db.collection.find().skip(5).limit(5) can be used Do paging, skip 5 pieces of data and then fetch 5 pieces of data
db.collection.find().count(true) count() returns the number of pieces of the result set
db.collection.find().skip(5).limit(5 ).count(true) When adding the skip() and limit() operations, to get the actual number of results returned, a parameter true is required, otherwise the total number of results that meet the query conditions will be returned

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217107&siteId=291194637