Windows uses MongoDB, and index creation

Install MongoDB

https://www.mongodb.com/download-center#community

 

 

 

Click the msi installer to install, you can customize the installation, select the installation location, I chose the D drive

 

Create a data\db directory in the root directory of the D drive to store data; create a data\dbConf\mongodb.log file to store MongoDB logs

 

Double click to run MongoDB server

 

 

 

 

Run the MongoDB server as a Windows service

Execute the following command to run the MongoDB server as a Windows service:

 

mongod.exe --bind_ip 127.0.0.1 --logpath "D:\data\dbConf\mongodb.log" --logappend --dbpath "D:\data\db" --port 27017 --serviceName "MyServiceName" --serviceDisplayName "MyServiceName" --install

The following table describes the parameters of mongodb startup:

parameter

describe

--bind_ip

Bind the service IP . If you bind to 127.0.0.1 , you can only access it locally. Do not specify all the default local IPs .

--logpath

Specify the MongoDB log file, note that the specified file is not a directory

--logappend

write log using append

--dbpath

Specify the database path

--port

Specify the service port number, the default port is 27017

--serviceName

Specify service name

--serviceDisplayName

Specify the service name, executed when there are multiple mongodb services.

--install

Specifies to install as a Windows service.

 

Installation of MongDB graphical tool ( Robomong )

 

 

http://www.softpedia.com/get/Internet/Servers/Database-Utils/Robomongo.shtml#download

 

 

Click Next to install

Simple and practical use of MongDB

Double click mongo.exe

 

 

 

1. Create a database

use DataBase_Name

If the database does not exist, create the database, otherwise switch to the specified database

2. View the database

show dbs

 

 

3. Insert the data document ( the set will be inserted directly, without the set, it will be created automatically )

db.name of collection.insert ({

name:' Zhang San ',

age:27

}

)

Eg:

 

 

 

4. Update documentation

db.collectionname.update ( _

{'name':' Zhang San '} , {$set:{'name':'lisi'}}

)

5. Inquiry

db.name of collection.find ()

6. Delete

Delete the collection named Zhangsan

db.col.remove({'name':' Zhang San '})

delete the first found record

db.name of collection.remove ( DELETION_CRITERIA,1)

delete all data

db.name of collection.remove ({})

6. Conditional query

db.name of collection.find ({likes:{$gte:100}})

similar

select  *  from col where likes>=100

less than ---$lt

Less than or equal to ----$lte

Explicit range query greater than 100 less than 200

db.name of collection.find ({likes:{$lt:100 ,$gt:100}})

7.limit query

db.COLLECTION_NAME.find().limit(NUMBER)

db.collection name.find({},{"title":1,_id:0}).limit(2)

8. Sort ( 1 ascending -1 descending)

db.setname.find().sort({KEY:1})

Comparison between MongoDB syntax and existing relational database SQL syntax

MongoDB syntax                                   MySql syntax

db.test.find({'name':'foobar'})<==> select * from test where name='foobar'

db.test.find()                            <==> select *from test

db.test.find({'ID':10}).count()<==> select count(*) from test where ID=10

db.test.find().skip(10).limit(20)<==> select * from test limit 10,20

db.test.find({'ID':{$in:[25,35,45]}})<==> select * from test where ID in (25,35,45)

db.test.find().sort({'ID':-1})  <==> select * from test order by IDdesc

db.test.distinct('name',{'ID':{$lt:20}})  <==> select distinct(name) from testwhere 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 testgroup by name

db.test.find('this.ID<20',{name:1})  <==> select name from test whereID<20

db.test.insert({'name':'foobar','age':25})<==>insertinto 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}})   <==> elete 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'

 

Fuzzy query: $regex

db.test.find({"name":{$regex:"aaa"}})

MongoDB index related

Check out the index:

db.getCollection('id_mapper').getIndexes()

 

Create index: 1 means create the index in ascending order, -1 means create the index in descending order

db.getCollection('id_mapper').ensureIndex({"contract_id":1},{background: true})

Note: If you create an index for a document with existing data, you can set the background value to true , so that MongoDB creates the index in the background, so that other operations will not be blocked during creation. In contrast, creating an index in a blocking way makes the entire creation process more efficient, but MongoDB will not be able to receive other operations during creation.

The default index name is: contract_id_1

 

Create a composite unique index:

db.getCollection('id_mapper').ensureIndex({"apply_id":1,"insti_code":1},{"background":true},{"unique":true})

 

Delete an index: Before deleting, be sure to see what the index name is

db.getCollection('id_mapper').dropIndex("contract_id_1")

db.getCollection('id_mapper').dropIndexes()    deletes all indexes

 

 

Double field query:

db.getCollection('id_mapper').find({"contract_id":"767862ce-0ca9-4673-92e5-c505d7d3686c"},{"insti_code":"1"})

There is a field with an index on the line

 

After the index is created, the newly added Document through the program will automatically add the index, which has been verified

 

Guess you like

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