创建数据库
switched to db mydb
查看所有的数据表
system.indexes
创建数据表
{"ok":1}
-- 说明
capped:true,表示该集合的结构不能被修改;
size:在建表之初就指定一定的空间大小,接下来 的插入操作会不断地按顺序APPEND数据在这个预分配好空间的文件中,如果已经超出空间大小,则回到文件头覆盖原来的数据继续插入。这种结构保证了插入 和查询的高效性,它不允许删除单个记录,更新的也有限制:不能超过原有记录的大小。这种表效率很高,它适用于一些暂时保存数据的场合,比如网站中登录用户 的session信息,又比如一些程序的监控日志,都是属于过了一定的时间就可以被覆盖的数据。
修改数据表名
{"ok":1}
数据表帮助主题help
> db.Account.help()
db.Account.find().help() - show DBCursor help
db.Account.count()
db.Account.dataSize()
db.Account.distinct( key ) - eg. db.Account.distinct( 'x' )
db.Account.drop() drop the collection
db.Account.dropIndex(name)
db.Account.dropIndexes()
db.Account.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
db.Account.reIndex()
db.Account.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
e.g. db.Account.find( {x:77} , {name:1, x:1} )
db.Account.find(...).count()
db.Account.find(...).limit(n)
db.Account.find(...).skip(n)
db.Account.find(...).sort(...)
db.Account.findOne([query])
db.Account.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
db.Account.getDB() get DB object associated with collection
db.Account.getIndexes()
db.Account.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.Account.mapReduce( mapFunction , reduceFunction , <optional params> )
db.Account.remove(query)
db.Account.renameCollection( newName , <dropTarget> ) renames the collection.
db.Account.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
db.Account.save(obj)
db.Account.stats()
db.Account.storageSize() - includes free space allocated to this collection
db.Account.totalIndexSize() - size in bytes of all the indexes
db.Account.totalSize() - storage allocated for all data and indexes
db.Account.update(query, object[, upsert_bool, multi_bool])
db.Account.validate() - SLOW
db.Account.getShardVersion() - only for use with sharding
查询一条记录
{
"_id" : ObjectId("4ded95c3b7780a774a099b7c"),
"UserName" : "libing",
"Password" : "1",
"Email" : "libing@126.cn",
"RegisterDate" : "2011-06-07 11:06:25"
}
--SELECT TOP 1 * FROM Account
查询聚集中字段的不同记录
--SELECT DISTINCT("UserName") FROM Account
查询聚集中UserName包含“keyword”关键字的记录
--SELECT * FROM Account WHERE UserName LIKE '%keyword%'
查询聚集中UserName以"keyword" 开头的记录
--SELECT * FROM Account WHERE UserName LIKE 'keyword%'
查询聚集中UserName以“keyword”结尾的记录
--SELECT * FROM Account WHERE UserName LIKE '%keyword'
查询聚集中指定列
--SELECT UserName,Email FROM Account
查询聚集中排除指定列
查询聚集中指定列,且Age > 20
--SELECT UserName,Email FROM Account WHERE Age > 20
聚集中字段排序
> db.Account.find().sort({"UserName":-1}) --降序
--SELECT * FROM Account ORDER BY UserName ASC
--SELECT * FROM Account ORDER BY UserName DESC
统计聚集中记录条数
--SELECT COUNT(*) FROM Account
统计聚集中符合条件的记录条数
-- SELECT COUNT(*) FROM Account WHERE Age > 20
统计聚集中字段符合条件的记录条数
--SELECT COUNT(UserName) FROM Account
查询聚集中前5条记录
--SELECT TOP 5 * FROM Account
查询聚集中第10条以后的记录
--SELECT * FROM Account WHERE AccountID NOT IN (SELECT TOP 10 AccountID FROM Account)
查询聚集中第10条记录以后的5条记录
--SELECT TOP 5 * FROM Account WHERE AccountID NOT IN (SELECT TOP 10 AccountID FROM Account)
or查询
--SELECT UserName,Email FROM Account WHERE UserName LIKE '%keyword%' OR Email LIKE '%keyword%'
添加新记录
修改记录
> db.Account.find({"AccountID":1})
{ "AccountID" : 1, "Age" : 27, "Email" : "libingql@163.com", "Password" : "1", "RegisterDate" : "2011-06-09 16:31:25", "UserName" : "libing", "_id" : ObjectId("4df08553188e444d001a763a") }
> db.Account.find({"AccountID":1})
{ "AccountID" : 1, "Age" : 28, "Email" : "libingql@163.com", "Password" : "1", "RegisterDate" : "2011-06-09 16:31:25", "UserName" : "libing", "_id" : ObjectId("4df08553188e444d001a763a") }
删除记录
> db.Account.remove({"Age":{$lte:20}}) --DELETE FROM Account WHERE Age <= 20
> db.Account.remove({"Age":{$gt:20}}) --DELETE FROM Account WHERE Age > 20
> db.Account.remove({"Age":{$gte:20}}) --DELETE FROM Account WHERE Age >= 20
> db.Account.remove({"Age":{$ne:20}}) --DELETE FROM Account WHERE Age != 20
> db.Account.remove({}) --全部删除