Last active
December 14, 2015 22:39
-
-
Save ialex32x/5159943 to your computer and use it in GitHub Desktop.
mongodb 基本命令
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
启动 mongodb: | |
> mongod --dbpath [dir] | |
# 使用安全认证、指定路径、指定日志地址 | |
> mongod --auth --dbpath /usr/mongo/data --logfile /var/mongo.log | |
# 关闭服务器 | |
> db.shutdownServer() | |
# 修复 | |
> mongod --repair | |
mongodb commands: | |
# switch db | |
> use [db_name] | |
# 查看所有user | |
> show users | |
# 查看所有db | |
> show dbs | |
# 查看所有collection | |
> show collections | |
# 查看用户 | |
db.system.users.find() | |
# 增加用户 | |
db.addUser('name', 'pwd') | |
# 用户认证 | |
db.auth('name', 'pwd') | |
# 删除用户 | |
db.removeUser('name') | |
# 当前数据库状态 | |
db.stats() | |
# 删除数据库 | |
db.dropDatabase() | |
# 删除当前集合 | |
db.[collection].drop() | |
# 查询 | |
db.[collection].find({条件}) | |
db.[collection].findOne({条件}) | |
# 删除 | |
db.[collection].remove({条件}) | |
# 添加 | |
db.[collection].save({值}) |
csv 格式
name,year
ABC,239
DEF,102
更新
db.foo.update
数组修改器:
$push 修改器
把 array_key 当做数组,把数据添加进去,若不存在该数据,就创建他
db.foo.update({"name": "who" }, {$push: {"array_key": {"key": value} } })
$ne 修改器
判断数据是不是已经存在了,不存在才进行操作
db.foo.update( {"name": "who", "array_key":{$ne:" zhangsan"}}, {$push: {"array_key": {"key": value} } })
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
导出数据
mongoexport -d page -c Article -q '{"spiderName": "mafengwoSpider"}' -f _id,title,content,images,publishDate,spiderName,url --jsonArray > mafengwoArticle.txt