利用dos命令操作mongodb数据库
1.需要在系统环境变量中配置好mongodb下的bin路径,配置再path里
2.正式开始启用mongdb数据表时,需要先启动mongdb服务,跟启动mysql服务一个道理
3.开启mongod服务,在mongdb的bin路径下输入以下命令:
C:\Users\Administrator>cd e:\MongoDB\mongodb-win32-x86_64-2.6.6/bin
C:\Users\Administrator>e:
e:\MongoDB\mongodb-win32-x86_64-2.6.6\bin>mongod --dbpath e:\MongoDB\mongodb-win
32-x86_64-2.6.6\data\db
2017-07-04T10:32:47.162+0800 [initandlisten] MongoDB starting : pid=2080 port=27
017 dbpath=e:\MongoDB\mongodb-win32-x86_64-2.6.6\data\db 64-bit host=USER-201702
16BV
2017-07-04T10:32:47.162+0800 [initandlisten] targetMinOS: Windows Server 2003 SP
2
2017-07-04T10:32:47.162+0800 [initandlisten] db version v2.6.6
2017-07-04T10:32:47.162+0800 [initandlisten] git version: 608e8bc319627693b04cc7
da29ecc300a5f45a1f
2017-07-04T10:32:47.162+0800 [initandlisten] build info: windows sys.getwindowsv
ersion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
BOOST_LIB_VERSION=1_49
2017-07-04T10:32:47.172+0800 [initandlisten] allocator: system
2017-07-04T10:32:47.172+0800 [initandlisten] options: { storage: { dbPath: "e:\M
ongoDB\mongodb-win32-x86_64-2.6.6\data\db" } }
2017-07-04T10:32:47.172+0800 [initandlisten] journal dir=e:\MongoDB\mongodb-win3
就是在bin路径下输入:mongod --dbpath (创建的db路径)
这个dos窗口就是启动的mongod服务,不能关闭,关闭后服务就断开了。
这个时候网页中输入:localhost:27017就会出现下面这个,表示服务开启成功,否则失败
It looks like you are trying to access MongoDB over HTTP on the native driver port.
4.然后新启动一个dos窗口,这个窗口就可以用来正式操作mongodb数据库了
5.新建一个自己的数据库:语法-》use [数据表名]
>
use demo
switched to db demo
6.往test中添加一条数据:db.[documentName].insert({json字符串})
> db.test.insert({name:"zhangsan",age:4})
WriteResult({ "nInserted" : 1 })
7.查询一条数据,查询所有数据,查询第一条数据,根据条件查询数据
语法:db.[documentName].find(query,projection),两个参数都是可选
db.[documentName].find():查询所有
db.[documentName].findOne():查询第一条数据
db.[documentName].findOne({"age":"4"}):只查询age等于4的第一条数据
db.[documentName].find({"name":"zhangsan"}):查询name等于张三的数据
db.[documentName].find({"name":"zhangsan","age":4})查询name等于张三并且age等于4的数据
> db.test.find()
{ "_id" : ObjectId("595b00d5433fef7d6b6f441f"), "name" : "zhangsan", "age" : 4 }
> db.test.findOne()
{
"_id" : ObjectId("595b00d5433fef7d6b6f441f"),
"name" : "zhangsan",
"age" : 4
}
> db.test.find({"name":"zhangsan"})
{ "_id" : ObjectId("595b00d5433fef7d6b6f441f"), "name" : "zhangsan", "age" : 4 }
> db.test.find({"name":"zhangsan","age":4})
{ "_id" : ObjectId("595b00d5433fef7d6b6f441f"), "name" : "zhangsan", "age" : 4 }
8.修改数据:先查询在修改$set:{json语句}:语法-》db.[documentName].update({json查询语句},{$set:{json修改语句}});
> db.test.update({name:zhangsan},{$set:{name:lisi}})
2017-07-04T10:55:35.336+0800 ReferenceError: zhangsan is not defined
> db.test.update({"name":"zhangsan"},{$set:{"name":"lisi"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
9.删除数据:先查询在删除:db.[documentName].remove({json删除语句})
>db.test.find()
{ "_id" : ObjectId("595b00d5433fef7d6b6f441f"), "name" : "lisi", "age" : 4 }
> db.test.remove({"name":"lisi"})
WriteResult({ "nRemoved" : 1 })
下面的db.test表示数据表,对数据表的一系列操作
> db.test.help()
DBCollection help
db.test.find().help() - show DBCursor help
db.test.count()
db.test.copyTo(newColl) - duplicates collection by copying all documents
to newColl; no indexes are copied.
db.test.convertToCapped(maxBytes) - calls {convertToCapped:'test', size:
maxBytes}} command
db.test.dataSize()
db.test.distinct( key ) - e.g. db.test.distinct( 'x' )
db.test.drop() drop the collection
db.test.dropIndex(index) - e.g. db.test.dropIndex( "indexName" ) or db.t
est.dropIndex( { "indexKey" : 1 } )
db.test.dropIndexes()
db.test.ensureIndex(keypattern[,options]) - options is an object with th
ese possible fields: name, unique, dropDups
db.test.reIndex()
db.test.find([query],[fields]) - query is an optional query filter. fiel
ds is optional set of fields to return.
e.g. db.test.find( {x:77}
, {name:1, x:1} )
db.test.find(...).count()
db.test.find(...).limit(n)
db.test.find(...).skip(n)
db.test.find(...).sort(...)
db.test.findOne([query])
db.test.findAndModify( { update : ... , remove : bool [, query: {}, sort
: {}, 'new': false] } )
db.test.getDB() get DB object associated with collection
db.test.getPlanCache() get query plan cache associated with collection
db.test.getIndexes()
db.test.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
db.test.insert(obj)
db.test.mapReduce( mapFunction , reduceFunction , <optional params> )
db.test.aggregate( [pipeline], <optional params> ) - performs an aggrega
tion on a collection; returns a cursor
db.test.remove(query)
db.test.renameCollection( newName , <dropTarget> ) renames the collectio
n.
db.test.runCommand( name , <options> ) runs a db command with the given
name where the first param is the collection name
db.test.save(obj)
db.test.stats()
db.test.storageSize() - includes free space allocated to this collection
db.test.totalIndexSize() - size in bytes of all the indexes
db.test.totalSize() - storage allocated for all data and indexes
db.test.update(query, object[, upsert_bool, multi_bool]) - instead of tw
o flags, you can pass an object with fields: upsert, multi
db.test.validate( <full> ) - SLOW
db.test.getShardVersion() - only for use with sharding
db.test.getShardDistribution() - prints statistics about data distributi
on in the cluster
db.test.getSplitKeysForChunks( <maxChunkSize> ) - calculates split point
s over all chunks and returns splitter function
db.test.getWriteConcern() - returns the write concern used for any opera
tions on this collection, inherited from server/db if set
db.test.setWriteConcern( <write concern doc> ) - sets the write concern
for writes to the collection
db.test.unsetWriteConcern( <write concern doc> ) - unsets the write conc
ern for writes to the collection
本文介绍如何通过DOS命令行来操作MongoDB数据库,包括启动服务、创建数据库、增删改查等基本操作。

6万+

被折叠的 条评论
为什么被折叠?



