Mongodb操作详解(一)
Mongodb操作详解(一)
C:\Users\Administrator>F:
F:\>cd Mongodb206
F:\Mongodb206>cd bin
F:\Mongodb206\bin>mongo.exe
MongoDB shell version: 2.0.6
connecting to: test
> show dbs; //显示所有的数据库,相当于mysql的show databases;
local (empty) //只有一个local数据库(无表)
> use local //选择local数据库
switched to db local www.zzzyk.com
> show tables; //show tables
> //无表显示
> post = {"title":"My Blog Post",
... "content":"Here's my blog post.",
... "created":new Date()}
//post字典变量,是javascript语句,如new Date()
//其实shell运行环境支持javascript语言,
{
"title" : "My Blog Post",
"content" : "Here's my blog post.",
"created" : ISODate("2012-08-11T11:38:08.698Z")
}
> db.blog.insert(post)
//把post数据输入local数据库中blog(数据库中
//不存在就自动建立)集合(即表)中
> db.blog.find() //将blog中数据全部找出,相当与select * from blog;
{ "_id" : ObjectId("502644352d31a25926fc6e85"), "title" : "My Blog Post", "conte
nt" : "Here's my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z") }
>db.blog.findOne() //只查找集合(表)中一个文档(行)数据
{ "_id" : ObjectId("502644352d31a25926fc6e85"), "title" : "My Blog Post", "conte
nt" : "Here's my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z") }
更新 update
> post.comments=[] //给之前的那个post字典变量添加一个comments键值
[ ]
> db.blog.update({title:"My Blog Post"},post)
//update()至少接受两个参数,第一个为限制条件,第二个为更改内容
//执行这个后,原文档(行)添加一个字段comments值为空。
> db.blog.find()
{ "_id" : ObjectId("502644352d31a25926fc6e85"), "title" : "My Blog Post", "conte
nt" : "Here's my blog post.", "created" : ISODate("2012-08-11T11:38:08.698Z"), "
comments" : [ ] }
> www.zzzyk.com
删除 remove delete
> db.blog.remove({title:"My Blog Post"})
//删除title为这个的一文档(行)数据
帮助 help
> help //查询mongo的javascript shell的帮助
db.help() help on db methods
db.mycoll.help() help on collection methods
rs.help() help on replica set methods
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries wit
h time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memor
y, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to f
urther iterate
DBQuery.shellBatchSize = x set default number of items to display on s
hell
exit quit the mongo shell
> db.help() //查询数据库级帮助
DB methods:
db.addUser(username, password[, readOnly=false])
db.auth(username, password) www.zzzyk.com
db.cloneDatabase(fromhost)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb, todb, fromhost)
db.createCollection(name, { size : ..., capped : ..., max : ... } )
db.currentOp() displays the current operation in the db
db.dropDatabase()
db.eval(func, args) run code server-side
db.getCollection(cname) same as db['cname'] or db.cname
db.getCollectionNames()
db.getLastError() - just returns the err msg string
db.getLastErrorObj() - return full status object
db.getMongo() get the server connection object
db.getMongo().setSlaveOk() allow this connection to read from the nonmas
ter member of a replica pair
db.getName()
db.getPrevError()
db.getProfilingLevel() - deprecated
db.getProfilingStatus() - returns if profiling is on and slow threshold
db.getReplicationInfo()
db.getSiblingDB(name) get the db at the same server as this one
上一个:Mongodb操作详解(二)
下一个:2012年NOSQL学习笔记之三
- 更多mongodb疑问解答:
- 【急】MongoDB写入错误~~~~
- Mongodb NOSql 数据库问题,是否可以插入带接口的类
- java操作mongodb
- Spring data MongoDB 更新整个内嵌文档时报错???????
- node.js连接mongodb更新
- MongoDB Java驱动 WriteConcern.SAFE非常浪费资源
- 求科普,hibernate怎样操作mongodb?
- 问一下mongodb怎么用hibernate整合
- mongodb查询的数据过多
- 使用JAVA创建MongoDB的问题
- Mongodb事务管理问题?
- mongodb利用java进行模糊查询
- spring 抽象类 注入值为空(spring3+mongodb+morphia)
- 【急】MongoDB写入错误~~~~
- Mongodb NOSql 数据库问题,是否可以插入带接口的类