王筝的博客
ruby学习

http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/?_ga=1.25950975.395536525.1415167309

 

(1)Import the public key used by package management system

The Ubuntu package management tools ensure package consistency and authenticity by requiring that

distributors sign packages with GPG keys. Issue the following command to import the MongoDB public

GPG key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

(2)Create a list file for MongoDB

Create the /etc/apt/sources.list.d/mongdb-org-3.0.list list file using the following command:

echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

(3)Reload local package database

Issue the following command to reload the local package database:

sudo apt-get update

(4)Install the MongoDB package

You can install either the lastest version of MongoDB or a specific version of MongoDB

You can install the latest stable version of MongoDB

sudo apt-get install -y mongodb-org

Or, install a specific release of MongoDB

 

sudo apt-get install -y mongodb-org=3.0.5 mongodb-org-server=3.0.5 mongodb-org-shell=3.0.5 mongodb-org-mongos=3.0.5 mongodb-org-tools=3.0.5

 

 

复制代码
    next_played_videos = @channel.videos.active.where(:begin_time.gt => begin_time).asc(:begin_time) 
    next_played_videos.each do |video|
      video.begin_time = begin_time   
      video.end_time = video.begin_time.to_i + video.duration
      begin_time = video.end_time     
      video.save
    end
复制代码

这里使用 each 操作遍历效率比较低,可以使用 Mongoid 的 inc 方法批量对指定的字段值进行增减,文档见 http://mongoid.org/en/mongoid/docs/persistence.html。

next_played_videos = @channel.videos.active.where(:begin_time.gt => begin_time).asc(:begin_time)
next_played_videos.inc(begin_at: -@video.duration, end_at: -@video.duration)

 

http://docs.mongodb.org/manual/reference/command/renameCollection/

db.source-namespace.renameCollection( "target" )
比如把users 替换成 all_users
db.users.renameCollection( "all_users" )

angela@angeladeMacBook-Air:/data/db$mongo
MongoDB shell version: 2.6.1
connecting to: test
2014-06-07T11:13:21.349+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2014-06-07T11:13:21.350+0800 Error: couldn’t connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

 

mongo数据库数据存放的默认位置是/data/db

angela@angeladeMacBook-Air:/data/db$rm mongod.lock

angela@angeladeMacBook-Air:/data/db$mongod --dbpath="/data/db"
[root@a02]$show dbs;
changhong_tv_cms 0.078GB

[root@a02]$ mongodump -d changhong_tv_cms -o
/temp/db_mongo.20140521.mongo

/workspace/db_baks$ scp -r -P 22022
root@10.xxx.xx.xx:/temp/db_mongo_changhong.20140521.mongo .

/workspace/db_baks$ mongorestore --directoryperdb
db_mongo_changhong.20140521.mongo

$ mongodump –help

options:
–help produce help message
-v [ –verbose ] be more verbose (include multiple times for more
verbosity e.g. -vvvvv)
–version print the program’s version and exit
-h [ –host ] arg mongo host to connect to ( <set name>/s1,s2 for
sets)
–port arg server port. Can also use –host hostname:port
–ipv6 enable IPv6 support (disabled by default)
-u [ –username ] arg username
-p [ –password ] arg password
–dbpath arg directly access mongod database files in the given
path, instead of connecting to a mongod server –
needs to lock the data directory, so cannot be used
if a mongod is currently accessing the same path
–directoryperdb if dbpath specified, each db is in a separate
directory
–journal enable journaling
-d [ –db ] arg database to use
-c [ –collection ] arg collection to use (some commands)
-o [ –out ] arg (=dump) output directory or “-” for stdout
-q [ –query ] arg json query
–oplog Use oplog for point-in-time snapshotting
–repair try to recover a crashed database
–forceTableScan force a table scan (do not use $snapshot)

-h 导出源
-d 要导出的数据库名称
-o 数据库要导出的位置