The simple construction of MongoDB

Source: Internet
Author: User
Tags exit in

Create log and data directories
mkdir Log
mkdir data

New log file
Touch Mongodb.log

The fork parameter that the daemon mode starts can also be configured in the configuration file
port=27017
dbpath=data/db
Logpath=log/mongodb.log
Logappend=true
Fork=true
Master = True


./mongod--dbpath/home/mongodb/mongodb/data--logpath/home/mongodb/mongodb/log/mongodb.log

To start a client connection
./mongodb
Exit
Enter exit in the shell

Related commands:
Show DBS show collections show Users show Profile show logs


If you want to create a database name <coc>
Use MyDB

To check the currently selected database use command:
Db

The database created by MyDB does not exist in the list. To display the database, you need to insert it into at least one file.
Db.movie.insert ({"Name": "Tutorials Yiibai"})

Create master.conf

#master. conf
Dbpath=/home/mongodb/mongodb/data/master
Logpath=/home/mongodb/mongodb/log/master.log
Pidfilepath=/home/mongodb/mongodb/log/master.pid
Directoryperdb=true
Logappend=true
Replset=testrs
bind_ip=10.1.235.62
port=27017
oplogsize=10000
Fork=true
Noprealloc=true


Create
Slaver.conf
Dbpath=/home/mongodb/mongodb/data/slaver
Logpath=/home/mongodb/mongodb/log/slaver.log
Pidfilepath=/home/mongodb/mongodb/log/slaver.pid
Directoryperdb=true
Logappend=true
Replset=testrs
bind_ip=10.10.148.131
port=27017
oplogsize=10000
Fork=true
Noprealloc=true

Master-Slave node startup
./mongod--config/home/mongodb/mongodb/master.conf
./mongod--config/home/mongodb/slaver.conf


cfg={_id: "Testrs", Members:{_id:0,host: ' 10.1.235.62:27017 ', priority:2}, {_id:1,host: ' 10.1.235.61:27017 ', priority : 1}

cfg={_id: "Testrs", Members:{_id:0,host: ' 10.1.235.62:27017 ', priority:2}, {_id:1,host: ' 10.1.235.61:27017 ', priority : 1}]};


./mongo 10.1.235.62:27017

./mongo 10.1.235.61:27018

MongoDB vs. Traditional sql:

Db.users.find () SELECT * from Users

Db.users.find ({"Age": +}) SELECT * from the users where age = 27

Db.users.find ({"username": "Joe", "Age": +}) SELECT * from the users where "username" = "Joe" and age = 27

Db.users.find ({}, {"username": 1, "email": 1}) Select Username, email from users

Db.users.find ({}, {"username": 1, "_id": 0})//no case//Instant column filter is added, _id will also return; you must explicitly block _id return

Db.users.find ({"Age": {"$gte": +, "$lte": ()}) select * from the users where age >=18 and age <=//$lt (<) $l Te (<=) $gt (>) $gte (>=)

Db.users.find ({"username": {"$ne": "Joe"}}) select * from users where username <> "Joe"

Db.users.find ({"Ticket_no": {"$in": [725, 542, 390]}) SELECT * from the users where Ticket_no in (725, 542, 390)

Db.users.find ({"Ticket_no": {"$nin": [725, 542, 390]}) SELECT * from the users where ticket_no not in (725, 542, 390)

Db.users.find ({"$or": [{"Ticket_no": 725}, {"Winner": True}]}) select * Form users where Ticket_no = 725 or winner = TR Ue

Db.users.find ({"Id_num": {"$mod": [5, 1]}}) select * from Users where (id_num mod 5) = 1

Db.users.find ({"$not": {"Age": +}}) select * from the users where not (age = 27)

Db.users.find ({"username": {"$in": [null], "$exists": True}}) select * from the users where username is null//if directly through find ( {"username": null}) For enquiries, then the "no username" record is screened out

Db.users.find ({"Name":/joey?/i})//Regular query, value is an expression that conforms to Pcre

Db.food.find ({fruit: {$all: ["Apple", "Banana"}})//array of queries, field fruit, contains both "Apple" and "banana" records

Db.food.find ({"fruit.2": "Peach"})//array of queries, field fruit, 3rd (starting from 0) element is peach Record

Db.food.find ({' fruit ': {' $size ': 3}})//array of queries, the number of query arrays is 3 records, $size previously cannot be combined with other operators

Db.users.findOne (criteria, {"Comments": {"$slice": 10}})//array of queries, returning only the first 10 of the arrays comments, or {"$slice": -10}, {"$slice": [ 23, 10]}; Return the last 10, and the middle 10, respectively.

Db.people.find ({"Name.first": "Joe", "Name.last": "Schmoe"})//nested query

Db.blog.find ({"Comments": {"$elemMatch": {"author": "Joe", "score": {"$gte": 5}}})//nested query, only if nested elements are arrays are used,

Db.foo.find ({"$where": "this.x + this.y = = 10"})//complex query, $where of course is very convenient, but inefficient. For complex queries, the order of consideration should be regular---MapReduce--$where

Db.foo.find ({"$where": "function () {return this.x + This.y = = Ten;}"})//$where can support JavaScript functions as query criteria

Db.foo.find (). Sort ({"X": 1}). Limit (1). Skip (10); Return to article (10, 11], sorted by "X"; The order of three limit is arbitrary, should try to avoid using Large-number in Skip,

The simple construction of MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.