Skip to content

MongoDB

changwu edited this page Mar 10, 2016 · 1 revision

安裝

$ brew install mongodb

設定檔路徑

/usr/local/etc/mongod.conf

systemLog:
    destination: file
    path: "/usr/local/var/log/mongodb/mongo.log"
    logAppend: true
storage:
    dbPath: /usr/local/var/mongodb
net:
    bindIp: 127.0.0.1
    port: 27017
  • The databases are stored in the /usr/local/var/mongodb/ directory
  • The mongod.conf file is here: /usr/local/etc/mongod.conf
  • The mongo logs can be found at /usr/local/var/log/mongodb/
  • The mongo binaries are here: /usr/local/Cellar/mongodb//bin

啟動

To have launchd start mongodb at login:
  ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
Then to load mongodb now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
Or, if you don't want/need launchctl, you can just run:
  mongod --config /usr/local/etc/mongod.conf

sudo mongod --config /usr/local/etc/mongod.conf --fork

進入資料庫

$ mongo

MongoDB shell version: 3.0.7
connecting to: test
> use test
switched to db test
> db.things.save( {a:1, b:2, c:3} )
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("562d9104e6195431313b5bea"), "a" : 1, "b" : 2, "c" : 3 }
> db.things.save( {a:3, b:4, c:6, d:200} )
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("562d9104e6195431313b5bea"), "a" : 1, "b" : 2, "c" : 3 }
{ "_id" : ObjectId("562d91b5e6195431313b5beb"), "a" : 3, "b" : 4, "c" : 6, "d" : 200 }
> db.things.find( {a:1} )
{ "_id" : ObjectId("562d9104e6195431313b5bea"), "a" : 1, "b" : 2, "c" : 3 }
> db.things.save( {a:1, b:2, fruit: [ "apple", "orange", "pear" ]} )
WriteResult({ "nInserted" : 1 })
> db.things.find()
{ "_id" : ObjectId("562d9104e6195431313b5bea"), "a" : 1, "b" : 2, "c" : 3 }
{ "_id" : ObjectId("562d91b5e6195431313b5beb"), "a" : 3, "b" : 4, "c" : 6, "d" : 200 }
{ "_id" : ObjectId("562d924ee6195431313b5bec"), "a" : 1, "b" : 2, "fruit" : [ "apple", "orange", "pear" ] }
> db.things.find().pretty()
{ "_id" : ObjectId("562d9104e6195431313b5bea"), "a" : 1, "b" : 2, "c" : 3 }
{
	"_id" : ObjectId("562d91b5e6195431313b5beb"),
	"a" : 3,
	"b" : 4,
	"c" : 6,
	"d" : 200
}
{
	"_id" : ObjectId("562d924ee6195431313b5bec"),
	"a" : 1,
	"b" : 2,
	"fruit" : [
		"apple",
		"orange",
		"pear"
	]
}

安裝 Bottle framework

$ pip install bottle
$ pip install pymongo
$ python hello_bottle.py

hello_bottle.py

from bottle import route, run, template


@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>', name=name)

run(host='localhost', port=8080)

復原資料

$ mongorestore dump
function ( obj , opts ){
    if ( obj == null )
        throw Error("can't save a null");

    if ( typeof( obj ) == "number" || typeof( obj) == "string" )
        throw Error("can't save a number or string");

    if ( typeof( obj._id ) == "undefined" ){
        obj._id = new ObjectId();
        return this.insert( obj , opts );
    }
    else {
        return this.update( { _id : obj._id } , obj , Object.merge({ upsert:true }, opts));
    }
}
for (i = 0; i < 1000; i++) { 
    names = ["exam", "essay", "quiz"]; 
    for (j = 0; j < 3; j++) { 
        db.scores.insert( { 
            "student": i, 
            "type": names[j], 
            "score": Math.round(Math.random()*100) 
        }); 
    }
}

db.scores.find({
    'type': 'essay',
    'score': 50,
}, {
    'student': true,
    '_id': false,
})

db.catalog.find (
    { "price": { "$gt": 10000 }, "reviews.rating": { "$gte": 5 } } 
)

db.scores.count( { "type": "essay", score: {"$gt": 90}} )


db.users.update( { "_id": "myrnarackham" }, { $set: {"country" : "RU" }})

db.scores.update( {score: {"$lt": 70}}, {"$inc": {"score": 20}}, {multi: true} )

db.students.explain().find({name:"aimee Zank"}); db.students.getIndexs(); db.students.dropIndex({name:"aimee Zank"});

Clone this wiki locally