撰写报告实验名称:课程名称:实验学期:班级名称:学生姓名:学生学号:上课地点:实验时间:截止时间:实验目的:实验指导书:实验内容:一、数据库操作1.显示当前选择的数据库> dbtest2.创建或切换数据库MyDB> use MyDBswitched to db MyDB> dbMyDB3.查看所有数据库> show dbslocal 0.078GB4.向MyDB插入一个文件> db.movie.insert({"name":"tutorials yiibai"}) WriteResult({ "nInserted" : 1 })5.查看所有数据库> show dbsMyDB 0.078GBlocal 0.078GB6.删除数据库MyDB> db.dropDatabase(){ "dropped" : "MyDB", "ok" : 1 }> show dbslocal 0.078GB二、集合操作1.显式和隐式创建集合movie显示集合创建> db.createCollection("movie"){ "ok" : 1 }> show collectionsmoviesystem.indexes隐身集合创建> db.movie.insert({"name":"tutorials yiibai"})WriteResult({ "nInserted" : 1 })> show collectionsmoviesystem.indexes2.查询所有集合> show collectionsmoviesystem.indexes3.查看集合总数据量> db.movie.count()14.查看movie集合所在数据库> db.movie.getDB()MyDB5.查看当前集合movie状态> db.movie.stats(){"ns" : "MyDB.movie","count" : 1,"size" : 112,"avgObjSize" : 112,"numExtents" : 1,"storageSize" : 8192,"lastExtentSize" : 8192,"paddingFactor" : 1,"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.","userFlags" : 1,"capped" : false,"nindexes" : 1,"totalIndexSize" : 8176,"indexSizes" : {"_id_" : 8176},"ok" : 1}6.集合movie重命名> db.movie.renameCollection("movies"){ "ok" : 1 }> show collectionsmoviessystem.indexes7.集合movie复制> db.movies.copyTo("movie")1> show collectionsmoviemoviessystem.indexes8.删除集合movie> db.movie.drop()true> show collectionsmoviessystem.indexes9.查看集合movie帮助> db.movies.help()DBCollection helpdb.movies.find().help() - show DBCursor helpdb.movies.count()db.movies.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.db.movies.convertToCapped(maxBytes) - calls {convertToCapped:'movies', size:maxBytes}} commanddb.movies.dataSize()db.movies.distinct( key ) - e.g. db.movies.distinct( 'x' )db.movies.drop() drop the collectiondb.movies.dropIndex(index) - e.g. db.movies.dropIndex( "indexName" ) or db.movies.dropIndex( { "indexKey" : 1 } )db.movies.dropIndexes()db.movies.ensureIndex(keypattern[,options])db.movies.explain().help() - show explain helpdb.movies.reIndex()db.movies.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.e.g. db.movies.find( {x:77} , {name:1, x:1} )db.movies.find(...).count()db.movies.find(...).limit(n)db.movies.find(...).skip(n)db.movies.find(...).sort(...)db.movies.findOne([query])db.movies.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )db.movies.getDB() get DB object associated with collectiondb.movies.getPlanCache() get query plan cache associated with collectiondb.movies.getIndexes()db.movies.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } ) db.movies.insert(obj)db.movies.mapReduce( mapFunction , reduceFunction , <optional params> ) db.movies.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursordb.movies.remove(query)db.movies.renameCollection( newName , <dropTarget> ) renames the collection.db.movies.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection namedb.movies.save(obj)db.movies.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})db.movies.storageSize() - includes free space allocated to this collection db.movies.totalIndexSize() - size in bytes of all the indexesdb.movies.totalSize() - storage allocated for all data and indexesdb.movies.update(query, object[, upsert_bool, multi_bool]) - instead of two flags, you can pass an object with fields: upsert, multidb.movies.validate( <full> ) - SLOWdb.movies.getShardVersion() - only for use with shardingdb.movies.getShardDistribution() - prints statistics about data distribution in the clusterdb.movies.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter functiondb.movies.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if setdb.movies.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collectiondb.movies.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection三、文档操作1.插入操作a.向集合movie中分别插入下列文档,并观察集合结构db.movie.insert({"name":"HuanLeSong","Times":10000})db.movie.insert({"name":"SanJie","Times":20000})db.movie.insert({"name":"SanJie","Address":"China"})db.movie.find()> db.movie.insert({"name":"HuanLeSong","Times":10000})WriteResult({ "nInserted" : 1 })> movieVar2 = {"name":"SanJie","Times":20000};{ "name" : "SanJie", "Times" : 20000 }> movieVar3 = {"name":"SanJie","Address":"China"};{ "name" : "SanJie", "Address" : "China" }> db.movie.find(){ "_id" : ObjectId("57503fd0af6dafef3d9ac421"), "name" : "tutorials yiibai" } { "_id" : ObjectId("57504012af6dafef3d9ac422"), "name" : "HuanLeSong", "Times" : 10000 }插入数据> db.movie.insert({"_id":08001,"name":"zhangsan"})WriteResult({ "nInserted" : 1 })> db.movie.find(){ "_id" : ObjectId("57503fd0af6dafef3d9ac421"), "name" : "tutorials yiibai" } { "_id" : ObjectId("57504012af6dafef3d9ac422"), "name" : "HuanLeSong", "Times" : 10000 }{ "_id" : 8001, "name" : "zhangsan" }> db.movie.save({"_id":08001,"name":"lisi"})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("57503fd0af6dafef3d9ac421"), "name" : "tutorials yiibai" } { "_id" : ObjectId("57504012af6dafef3d9ac422"), "name" : "HuanLeSong", "Times" : 10000 }{ "_id" : 8001, "name" : "lisi" }采用数组的方式 []>db.movie.insert([{username:'aaa',password:'bbb',tel:'123123123'},{username:'bb b',password:'ccc',tel:'123123234'}]);BulkWriteResult({"writeErrors" : [ ],"writeConcernErrors" : [ ],"nInserted" : 2,"nUpserted" : 0,"nMatched" : 0,"nModified" : 0,"nRemoved" : 0,"upserted" : [ ]})> db.movie.find(){ "_id" : ObjectId("57503fd0af6dafef3d9ac421"), "name" : "tutorials yiibai" } { "_id" : ObjectId("57504012af6dafef3d9ac422"), "name" : "HuanLeSong", "Times" : 10000 }{ "_id" : 8001, "name" : "lisi" }{ "_id" : ObjectId("57504306af6dafef3d9ac423"), "username" : "aaa", "password" : "bbb", "tel" : "123123123" }{ "_id" : ObjectId("57504306af6dafef3d9ac424"), "username" : "bbb", "password" : "ccc", "tel" : "123123234" }删除操作(1)按条件进行删除> db.movie.remove({"name":"HuanLeSong"});WriteResult({ "nRemoved" : 1 })> db.movie.find(){ "_id" : ObjectId("57503fd0af6dafef3d9ac421"), "name" : "tutorials yiibai" } { "_id" : 8001, "name" : "lisi" }{ "_id" : ObjectId("57504306af6dafef3d9ac423"), "username" : "aaa", "password" : "bbb", "tel" : "123123123" }{ "_id" : ObjectId("57504306af6dafef3d9ac424"), "username" : "bbb", "password" : "ccc", "tel" : "123123234" }(2)删除所有[用remove删除]> db.movie.remove({})WriteResult({ "nRemoved" : 4 })> db.movie.find()> show collectionsmovieSystem.indexes[用drop删除]>db.movie.insert([{username:'aaa',password:'bbb',tel:'123123123'},{username:'bb b',password:'ccc',tel:'123123234'}]);BulkWriteResult({"writeErrors" : [ ],"writeConcernErrors" : [ ],"nInserted" : 2,"nUpserted" : 0,"nMatched" : 0,"nModified" : 0,"nRemoved" : 0,"upserted" : [ ]})> db.movie.find(){ "_id" : ObjectId("575044fcaf6dafef3d9ac425"), "username" : "aaa", "password" : "bbb", "tel" : "123123123" }{ "_id" : ObjectId("575044fcaf6dafef3d9ac426"), "username" : "bbb", "password" : "ccc", "tel" : "123123234" }> db.movie.drop()true> show collectionssystem.indexes修改操作> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "bbb", "tel" : "123123123" }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "username" : "bbb", "password" :"ccc", "tel" : "123123234" }> db.movie.update({username:"bbb"},{password:"abc"});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "bbb", "tel" : "123123123" }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "password" : "abc" }$set修改器> db.movie.update({username:"aaa"},{$set:{password:"*****"}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "*****", "tel" : "123123123" }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "password" : "abc" }$unset修改器> db.movie.update({username:"aaa"},{$unset:{tel:1}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "*****" }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "password" : "abc" }.$inc修改器> db.movie.update({username:"aaa"},{$inc:{age:2}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "*****", "age" : 2 }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "password" : "abc" }在原基础上age+2> db.movie.update({username:'aaa'},{$inc:{age:2}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "*****", "age" : 4 }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "password" : "abc" }Upsert 的用法 --当没有文档符合更新条件,就会以这个条件创建新的文档> db.movie.update({username:'aaa'},{$set:{abc:123}},true)WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "*****", "age" : 4, "abc" : 123 }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "password" : "abc" }当文档符合更新条件,就会更新文档> db.movie.update({username:'aaa'},{$set:{abc:234}},true)WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })> db.movie.find(){ "_id" : ObjectId("575048d9af6dafef3d9ac427"), "username" : "aaa", "password" : "*****", "age" : 4, "abc" : 234 }{ "_id" : ObjectId("575048d9af6dafef3d9ac428"), "password" : "abc" }实验步骤与调试过程:实验结果:疑难小结:在进行查看当前集合状态的时候输出的结果有点不是很明白在插入数值时我们会发现如果插入的集合的“_id”值,在集合中已经存在则在插入_id是的值会报错出现错误。