show dbs 查看有哪些数据库
admin :从权限角度看这是root数据库,钥匙讲一个用户添加到这个数据库,这个用户自动继承所有数据库的权限
local:这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合
config:当mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息
use articledb 创建或者选择数据库
db 查看当前所在的库
db.dropDatabase() 删除当前数据库
集合操作
集合的显示创建
db.createCollection("listname")
show collections
集合隐式创建
插入数据的时候自动创建
集合删除
db.mycollection.drop()
正则的复杂条件查询
db.comment.find({userid:/3/}) 查询用户id包含3的数据
比较查询
db.collectionname.find({"field":{$gt:value}})//大于:field > value
db.collectionname.find({"field":{$lt:value}})//小于:field < value
db.collectionname.find({"field":{$gte:value}})//大于等于:field >= value
db.collectionname.find({"field":{$lte:value}})//小于等于:field <= value
db.collectionname.find({"field":{$ne:value}})//不等于:field != value
包含查询
db.comment.find({userid:{$in:["1003","1002"]}})
条件连接查询
db.comment.find({$and:[{likenum:{$gte:NumberInt(1000)}},{likenum:{$lte:NumberInt(2000)}}]})
db.comment.find({$or:[{userid:"1002"},{likenum:{$lte:NumberInt(1000)}}]})
索引
索引类型
单字段索引
复合索引
其他索引
地理空间索引 Geospatial Index
文本索引 Text Index
哈希索引 Hash Index