MongoDB entry notes,
Installation and configuration manual Installation
Curl-O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgztar-zxvf mongodb-linux-x86_64-3.0.6.tgzcd mongodb (mv mongodb-linux-x86_64-3.0.6.tgz mongodb) mkdir data log conf # create configuration directory cd confvim mongod. conf # create the configuration file port = 12345 dbpath = datalogpath = log/mongodb. logfork = true
Automatic Installation
Sudo apt-key adv -- keyserver hkp: // keyserver.ubuntu.com: 80 -- recv EA312927 # import keyecho "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee/etc/apt/sources. list. d/mongodb-org-3.2.list # create a MongoDB list sudo apt-get update # update sudo apt-get install-y mongodb-org # install # log file/var/lib/mongodb/mongd. log
Start MongoDB
# Manually install the startup method. /bin/mongod-f conf/mongod. conf # Start the Mongodb service.-f specifies the configuration file # tail log/mongo. log # You can see that the connection is waiting. /bin/mongo 127.0.0.1: 12345/test # connect to the local server/port number/database # automatically install and start the pgrep mongo-l # Check whether mongodb starts the service consumer D statusservice consumer D startservice consumer D stopservice consumer D restartmongo
Basic usage
Mongo-version # view version information use admindb. shutdownServer () # exit show dbs # display database use test # select/create database db. dropDatabase () # Delete the current database db # display the current database db. test_col.insert ({x: 1}) # insert (create set) var_insert = {a: 1, B: 3} db. test_col.insert (var_insert) # insert db as a variable. test_col.insert.pdf ([document1, document2,...]) # insert multiple db entries. test_col.drop () # Delete the test_col Collection show collections # display the collection db. test_col.find () # query all data. The id field is automatically generated and can be specified, but it cannot be repeated. test_col.find ({y: 1}) # query the specified database. test_col.find (). count () # count for (I = 3; I <10; I ++) db. test_col.insert ({x: I}) # supports shell loop db. test_col.find (). skip (2 ). limit (3 ). sort ({x: 1}) # Skip, limit, and sort db. test_col.update ({x: 1}, {$ set: {x: 10}) # partially update xdb. test_col.update ({y: 100}, {y: 999}, true) # true indicates that {y: 100} is inserted if {y: 999} does not exist. test_col.update ({x: 1}, {$ set: {x: 2 }}, false, true) # update the database with multiple records. test_col.remove ({x: 2}) # delete a database. test_col.remove ({}) # delete all data
Index
Four attributes: name, unique, spare, and expired Index
The default name value is key1_value1_key2_value2 _..., That is, key + "_" + value
Db. test_col.getIndexes () # view the index database. test_col.ensureIndex ({x: 1, y: 1 },{ name: "x" },{ unique: true}) # Set the index database. test_col.dropIndex ({x: 1, y: 1}) db. test_col.dropIndex ("x") # Delete the index database. test_col.insert ({x: 10, y: 1}) db. test_col.insert ({x: 10, y: 1}) # An error is returned. If unqiue is set to true, a unique db is required. test_col.ensureIndex ({x: 1}, {sparse: true}) # sparse: true: adds an index to the record regardless of whether the record contains x: 1. test_col.find ({x :{$ exists: true}) # Find the record db containing x. test_col.find ({x :{$ exists: false }}). hint ("x_1") # forcibly search by "x_1" Index
_ Id index: Default index of the set. Mongodb automatically generates a unique _ id field for each inserted data.
Single-Key Index: create a single-key indexdb.test_col.ensureIndex({x:1})
Multi-Key Index: Create a multi-Key Indexdb.test_col.ensureIndex({x:(1,2)})
Composite Index:db.test_col.ensureIndex({x:1, y:2})
Expired index:
db.test_col.ensureIndex({time:1}, {expireAfterSeconds:10})
,db.test_col.insert({time:new date()})
-The field value of the expired index must be an array of the specified time type IOSdata or IOSdata.
-Once the index expires, the corresponding data will be deleted, that is, the time: new date () data will be deleted 10 seconds later.
-If an IOSdata array is specified, the expired index cannot be a composite index starting from the latest installation time.
Full-text index
Only one full-text index can be created for each set.
Create a full-text index for a string or string array
Db. test_col.ensure ({"article": "text"}) db. test_col.find ({$ "text": {"$ search": "aa bb-cc"}) # spaces between aa bb indicate logic or, and-cc indicates no ccdb search. test_col.find ({$ "test": {$ "search": "\" aa \ "\" bb \"}}) # "aa" bb "uses" "to indicate logical and
Function
function add(r1, r2){ return r1+r2;}
Geographic location index
2D index representation: [longitude, latitude]; value range: longitude [-180,180], latitude [-90,90]
An error is reported if the longitude is invalid during insertion, but the latitude is invalid.
Db. location. ensureIndexes ({"w": "2d"}) # create a 2d geographic location index db. location. find ({w: {$ near: []}, $ maxDistance: 10}) # query the near distance by distance to [], and the maximum distance is 10 left and right coordinates db. location. find ({w: {$ geoWithin: {$ box: [[0, 0], [20, 20] }}) # geoWithin query by shape, box [[x0, y0], [x1, y1] indicates a rectangle.
-
Top
-
0
-
Step on
-
0
View comments