Show DBS; #查看数据库
IMOOC 0.203125GB
Local (empty)
Use imooc_2# This command is equivalent to creating this database when there is no imooc_2 database
Switched to DB imooc_2
Show tables# View the table under the database
System.indexes
Teacher
Db.teacher.insert ({' Name ': lilaoshi,age:24}) #当imooc_2数据库没有teacher表时这个命令相当于创建了此表并插入数据
Sat 20:54:09 Referenceerror:lilaoshi is not defined (shell): # because Lilaoshi is a string
Db.teacher.insert ({' name ': ' Lilaoshi ', age:24}) #插入
Db.teacher.find (); #查找 (_id is the default key for MongoDB)
{"_id": ObjectId ("55e1ab7ab6aebb3a7ca49928"), "name": "Lilaoshi", "Age": 24}
Use imooc# into the IMOOC database
Switched to DB IMOOC
Show tables; #查看数据库下的表
Goods
Imooc_collection
Stu
System.indexes
Tea
Db.stu.count () #查看数据量
10000
Db.stu.find ({_id:3})
{"_id": 3, "SN": "003", "name": "Xiaoming3"}
Db.stu.remove ({_id:3}) #删除数据根据_id删除
Db.stu.find ({_id:3})
Db.stu.remove () #删除全部数据
Db.stu.find ()
Db.stu.insert ({_id:2, ' name ': ' Xiaoming2 ', age:21})
for (i=3;i<30;i++) Db.stu.insert ({_id:i, ' name ': ' Xiaoming ' +i,age:2+i}) #用for插入多条
Db.stu.find ()
{"_id": 1, "name": "Xiaoming", "Age": 20}
{"_id": 2, "name": "Xiaoming2", "Age": 21}
{"_id": 3, "name": "Xiaoming3", "Age": 5}
Db.stu.update ({_id:2},{$set: {age:23}}) #修改_id为2的age
Db.stu.find ()
{"_id": 1, "name": "Xiaoming", "Age": 20}
{"_id": 2, "name": "Xiaoming2", "Age": 23}
for (i=1;i<20;i++) db.stu.update ({_id:i},{$set: {age:23}}) #批量修改
Db.stu.find ()
{"_id": 1, "name": "Xiaoming", "Age": 23}
{"_id": 2, "name": "Xiaoming2", "Age": 23}
{"_id": 3, "name": "Xiaoming3", "Age": 23}
{"_id": 4, "name": "Xiaoming4", "Age": 23}
{"_id": 5, "name": "Xiaoming5", "Age": 23}
Db.stu.help () #查看帮助
Dbcollection Help
Db.stu.find (). Help ()-Show dbcursor Help
Db.stu.count ()
Db.stu.copyTo (Newcoll)-duplicates collection by copying all documents to newcoll; No indexes is copied.
Db.stu.convertToCapped (MaxBytes)-Calls {converttocapped: ' Stu ', size:maxbytes}} command
Db.stu.dataSize ()
DB.STU.DISTINCT (key)-e.g Db.stu.distinct (' X ')
Db.stu.drop () Drop the collection
Db.stu.dropIndex (index)-e.g Db.stu.dropIndex ("IndexName") or Db.stu.dropIndex ({"Indexkey": 1})
Db.stu.dropIndexes ()
Db.stu.ensureIndex (Keypattern[,options])-Options is a object with these possible fields:name, unique, dropdups
Db.stu.reIndex ()
Db.stu.find ([query],[fields])-query is a optional query filter. Fields are optional set of fields to return.
e.g. db.stu.find ({x:77}, {name:1, x:1})
Db.stu.find (...). Count ()
Db.stu.find (...). Limit (n)
Db.stu.find (...). Skip (N)
Db.stu.find (...). Sort (...)
Db.stu.findOne ([query])
Db.stu.findAndModify ({update: ..., remove:bool [, query: {}, Sort: {}, ' new ': false]})
Db.stu.getDB () Get DB object associated with collection
Db.stu.getIndexes ()
Db.stu.group ({key: ..., initial: ..., reduce: ...) [, cond: ...] } )
Db.stu.insert (obj)
Db.stu.mapReduce (Mapfunction, Reducefunction,)
Db.stu.remove (query)
Db.stu.renameCollection (NewName,) renames the collection.
Db.stu.runCommand (name,) runs a DB command with the given name where the first param is the collection name
Db.stu.save (obj)
Db.stu.stats ()
Db.stu.storageSize ()-Includes free space allocated to this collection
Db.stu.totalIndexSize ()-size in bytes of all the indexes
Db.stu.totalSize ()-Storage allocated for all data and indexes
Db.stu.update (query, object[, Upsert_bool, Multi_bool])-instead of the flags, you can pass a object with Fields:upsert , Multi
Db.stu.validate ()-SLOW
Db.stu.getShardVersion ()-only if use with sharding
Db.stu.getShardDistribution ()-Prints statistics about data distribution in the cluster
Db.stu.getSplitKeysForChunks ()-calculates split points over all chunks and returns splitter function
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.