当前位置:数据库 > mongodb >>

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
 
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,