**
First, to install MongoDB
**
1.: Http://www.mongodb.org/downloads
2. Unzip to the directory you want to install , such as D:\mongodb
3. Create folders d:\mongodb\data\db, D:\mongodb\data\log, respectively, to install the DB and log files, create a log file under the log folder MongoDB.log, that is, d:\mongodb\ Data\log\mongodb.log
4. Run cmd.exe into the DOS command interface and execute the following command
> CD D:\mongodb\bin
> D:\mongodb\bin>mongod-dbpath "D:\mongodb\data\db"
If you see a similar message, the boot succeeds, and the default MongoDB listener port is 27017,mysql 3306
5. Test the connection
Open a new CMD window, enter the MongoDB bin directory, enter MONGO or Mongo.exe, the following message shows that the test passed, at this time we have entered the test database, how to enter the other database below will say.
Enter exit or CTRL + C to exit.
or :
新开一个cmd窗口,进入mongodb的bin目录,输入mongo --port 27017,出现如下信息说明测试通过,此时我们已经进入了test这个数据库,如何进入其他数据库下面会说。
Enter exit or CTRL + C to exit.
6. When the Mongod.exe is closed , Mongo.exe cannot connect to the database. So every time you want to use the MongoDB database 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, go to 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"
Here MongoDB.log is the log file that was started, –servicename the "MongoDB" service named MongoDB
Then start the MongoDB service
> D:\mongodb\bin>net START MongoDB
Open Task Manager and you can see that the process has started
7. Shutting down services and deleting processes
> D:\mongodb\bin>net Stop MongoDB (Close service)
> D:\mongodb\bin>mongod–dbpath "d:\mongodb\data\db" –logpath "D:\mongodb\data\log\MongoDB.log" –remove– ServiceName "MongoDB" (delete, note not –install)
or
Used in the shell: Use admin after Db.shutdownserver ()
**
So it's closed.
Ii. Use of MongoDB
**
1. Common commands
show dbs 显示数据库列表use dbname 进入dbname数据库,大小写敏感,没有这个数据库也不要紧show collections 显示数据库中的集合,相当于表格
2. Create & Add
db.users.save({"name":"lecaf"}) 创建了名为users的集合,并新增了一条{"name":"lecaf"}的数据db.users.insert({"name":"ghost", "age":10}) 在users集合中插入一条新数据,,如果没有users这个集合,mongodb会自动创建save()和insert()也存在着些许区别:若新增的数据主键已经存在,insert()会不做操作并提示错误,而save() 则更改原来的内容为新内容。 存在数据:{ _id : 1, " name " : " n1 "} ,_id是主键 insert({ _id : 1, " name " : " n2 " }) 会提示错误 save({ _id : 1, " name " : " n2 " }) 会把 n1 改为 n2 ,有update的作用。
3. Delete
db.users.remove() 删除users集合下所有数据db.users.remove({"name": "lecaf"}) 删除users集合下name=lecaf的数据db.users.drop()或db.runCommand({"drop","users"}) 删除集合usersdb.runCommand({"dropDatabase": 1}) 删除当前数据库
4. Find
db.users.find() 查找users集合中所有数据db.users.findOne() 查找users集合中的第一条数据
5. Modifications
db.users.update({"name":"lecaf"}, {"age":10}) 修改name=lecaf的数据为age=10,第一个参数是查找条件,第二个参数是修改内容,除了主键,其他内容会被第二个参数的内容替换,主键不能修改,
**
Third, advanced applications
**
1. Condition Lookup
Db.collection.find ({"Key": Value}) finds the Key=value data Db.collection.find ({"key": {$gt: Value}}) key > Valuedb. Collection.find ({"key": {$lt: Value}}) key < Valuedb.collection.find ({"key": {$gte: Value}}) key >= Valuedb.collection.find ({"key": {$lte: Value}}) key <= Valuedb.collection.find ({"key": {$gt: value1, $lt: V Alue2}}) value1 < key <value2db.collection.find ({"key": {$ne: Value}}) key <> valuedb.collection. Find ({"key": {$mod: [10, 1]}) takes a modulo operation with the condition equivalent to key% 10 = = 1 i.e. key divided by 10 Db.collection.find ({"key": {$nin: [1, 2, 3]}) does not belong, the condition equal to the value of key does not belong to [1, 2, 3] any one of the Db.collection.find ({"key": {$in: [1, 2, 3]}) belongs to, the condition equals key equals [1, 2, 3] any one of the Db.collection.find ({"key": {$size: 1}}) $size number, size, condition equal to the number of values of key is 1 (key must be an array, a value is not considered as an array of 1) Db.colle Ction.find ({"key": {$exists: True|false}}) $exists field exists, true returns data with field key, FALSE returns data db.collection.find with no word key ({" Key ":/^val.*val$/i}) Regular, similar like; "I" Ignores case, "M" supports multiline Db.collection.find ({$or: [{a:1}, {b:2}]}) $or or (note: MongoDB 1.5.3 later version is available), eligible a=1 or compliant The data of the condition b=2 will be queried Db.collection.find ({"Key": Value, $or: [{a:1}, {b:2}]}) meets the conditional key=value, and conforms to any of the other two conditions in the data db. Collection.find ({"Key.subkey": Value}) the values in the inline object match, note: "Key.subkey" must be quoted Db.collection.find ({"key": {$not:/^val.*val$ /i}}) This is an operator that is used in combination with other query conditions and is not used alone. The result set obtained by the above query condition plus $not can get the opposite set.
2. Sorting
db.collection.find().sort({ "key1" : -1 ,"key2" : 1 }) 这里的1代表升序,-1代表降序
3. Other
db.collection.find().limit(5) 控制返回结果数量,如果参数是0,则当作没有约束,limit()将不起作用db.collection.find().skip(5) 控制返回结果跳过多少数量,如果参数是0,则当作没有约束,skip()将不起作用,或者说跳过了0条db.collection.find().skip(5).limit(5) 可用来做分页,跳过5条数据再取5条数据db.collection.find().count(true) count()返回结果集的条数db.collection.find().skip(5).limit(5).count(true) 在加入skip()和limit()这两个操作时,要获得实际返回的结果数,需要一个参数true,否则返回的是符合查询条件的结果总数
Windows MongoDB installation and use collation