mongodb操作.docx_第1页
mongodb操作.docx_第2页
mongodb操作.docx_第3页
mongodb操作.docx_第4页
mongodb操作.docx_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

撰写报告窗体顶端实验名称:课程名称:实验学期:班级名称:学生姓名:学生学号:上课地点:实验时间:截止时间:实验目的:实验指导书: 实验内容:一、数据库操作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 : 16. 集合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 , )db.movies.aggregate( pipeline, ) - performs an aggregation on a collection; returns a cursordb.movies.remove(query)db.movies.renameCollection( newName , ) renames the collection.db.movies.runCommand( name , ) 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: , indexDetailsName: )db.movies.storageSize() - includes free space allocated to this collectiondb.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( ) - SLOWdb.movies.getShardVersion() - only for use with shardingdb.movies.getShardDistribution() - prints statistics about data distribution in the clusterdb.movies.getSplitKeysForChunks( ) - 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( ) - sets the write concern for writes to the collectiondb.movies.unsetWriteConcern( ) - 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:bbb,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:bbb,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 :

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论