win下使用mongodb

1,下载mongodb

http://www.mongodb.org/display/DOCS/Downloads

2,解压就行

3,启动mongodb:

      bin/mongod run --auth --dbpath c:/data/db(服务端)

      bin/mongo (客户端)

 

     如果要了解详细的启动参数可以在命令行输入bin/mongodb /?查看

4,使用:

  >show  dbs

  >use post (post 为其中一个db)

  >show collections (post下所有表)

数据库操作:

保存数据:db.collection.save()

更新数据:db.collection.update()

删除数据:db.collection.remove()

 

删除collectiondb.collection.drop()

 

 

增:

db.posts.insert({x:1})

db.posts.insert( obj , _allow_dot )

_allow_dot大概就是判断这个obj是否合法的,如是否包含".","$"

 

db.posts.save({x:1})

db.posts.save(obj)

关于这个save得说一下:其实它和insert差不多,if  typeof( obj._id ) == "undefined": 就调用insert , else: 调用 update( { _id : obj._id } , obj , true

删:

db.posts.remove(t, justone)

 

 

查:

db.posts.find({x:1},{y:true},1,1)

db.posts.find(query,fields,limit,skip)

query--查询条件  fields--所需字段  limit--记录数  skip--从几条开始

db.posts.findOne({x:1},{y:true})

db.posts.findOne(query,fields)

db.posts.find({x:1}).skip(2).limit(3).sort({x:1})

db.posts.distinct('x')  select distinct x from posts

db.posts.count()

db.posts.find({x, {gt:2}})  查找比2大的x

db.posts.find({x: {$exists:true}})

* 支持模糊查询:db.posts.find({x:{'$regex': '/S*a/S*'}}) --查找包含字母'a'的x

 

改:

关于update这个方法得重点说一下:

update posts set y=66 where x=1:

db.posts.update({x:1},{$set:{y:66}}, false, true)

db.posts.update( query , obj , upsert , multi ) 解释:

query--查询条件  obj--结果 

upsert-- true: 如果没有符合条件的纪录就插入

       false: 如果有条符合条件的记录就修改(默认false

multi-- true: 符合条件的所有记录都要修改

      false: 只修改第一条记录(默认false)

 

 

 

比较操作符:

$gt $lt $gte $lte $ne

db.collection.find({ "field" : { $gt: value1, $lt: value2 } } );   

$all表示满足条件的元素的最小集合

$exists表示是否存在db.posts.find({x:{$exists:true}})

$mod取模运算db.posts.find({x:{$mod:[2,1]}})

$in $nin   db.posts.find({x:{$in:[1,2,3]}})

$or $nor $not

 

 

 

关于索引:

(由于下面内容有用到db中的collection,现假设为posts

 

1,查看所有索引:

db.posts.getIndexes()

2,创建索引:

db.posts.ensureIndex({x:1})  (x表示为一字段)

建立组合索引:

db.posts.ensureIndex({x:1,y:1}) 

<对二级字段创建组合索引也一样db.posts.ensureIndex({x.a:1, x.b:1})>

       1表示升序,-1表示降序

3db.posts.find().sort({x:1})

4,惟一索引:

db.posts.ensureIndex({x:1},{unique:true})

5,删除索引:

db.posts.dropIndexes()

想选择性删除,在括号里加参数,如:db.posts.dropIndex({x:1})

6,怎样解决已存在重复值,但还想加惟一索引:

db.posts.ensureIndex({x:1},{unique:true, dropDups:true})

虽然会出错,但索引是建成了

 

 

 createIndex()ensureIndex()暂时还没有看出来有什么区别。。

 

 

参考资料:

http://desert3.iteye.com/blog/856820

还有其它各种文档,非常感谢!

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值