本文共 4205 字,大约阅读时间需要 14 分钟。
show dbs show database names
> show dbslocal (empty)logging 0.203125GBtest 0.203125GB
show collections show collections in current database
> show collectionsbiossystem.indexes
另一种用法是show tables
> show tablesbiossystem.indexes
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
> show profiledb.system.profile is emptyUse db.setProfilingLevel(2) will enable profilingUse db.system.profile.find() to show raw profile entries
useset curent database to > use loggingswitched to db logging
存储嵌套的对象
db.foo.save({'name':'neo','address':{'city':'shenzhen','post':518000},'phone':[13113668890,13322993040]})
存储数组对象
db.foo.save({'Uid':'netkiller@msn.com','phone':['13322993040','13113668890']})
db.bios.insert( { _id: 1, name: { first: 'John', last: 'Backus' }, birth: new Date('Dec 03, 1924'), death: new Date('Mar 17, 2007'), contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ], awards: [ { award: 'W.W. McDowell Award', year: 1967, by: 'IEEE Computer Society' }, { award: 'National Medal of Science', year: 1975, by: 'National Science Foundation' }, { award: 'Turing Award', year: 1977, by: 'ACM' }, { award: 'Draper Prize', year: 1993, by: 'National Academy of Engineering' } ] })
根据query条件修改,如果不存在则插入,允许修改多条记录
db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
删除uid=10的记录
db.foo.remove({'uid':10})
删除所有的记录
db.foo.remove()
db.foo.remove({ "_id" : ObjectId("56e10b66a22ef1b1408b4567")})
db.collection.drop()
> db.access.count()51528> db.access.count()104401
group()类似SQL中的Group by
> db.test.group({key: {remote_addr: true}, initial: {count: 0}, reduce: function(obj, prev) {prev.count++}});[ { "remote_addr" : "192.168.2.76", "count" : 3 }, { "remote_addr" : "192.168.2.70", "count" : 1 }]
查找所有 所有记录
db.foo.find() list objects in collection foodb.foo.find( { a : 1 } ) list objects in foo where a == 1
查找一条记录
db.foo.findOne()
根据条件检索10条记录
db.foo.find({'name':'neo'}).limit(10)
sort排序
db.foo.find({'name':'neo'}).sort({'Dt',-1})db.foo.find().sort({'Ct':-1}).limit(1)
count记录统计操作
db.foo.count()
distinct操作,去重复查询指定列,
db.foo.distinct('name')
”>=”操作
db.foo.find({"timestamp": {"$gte" : 2}})
子对象的查找
db.foo.find({'address.city':'shenzhen'})
db.getCollection('tracker').find({name:"81004892"})
db.getCollection('pyramidSelling').find({},{'phone':1})
db.getCollection('pyramidSelling').find({},{'phone':0})
db.getCollection('tracker').find({name:"81004892"}).sort({ctime: -1})
cat data.bson | mongo test
关闭MongoDB数据库
db.shutdownServer()
{ "_id" : ObjectId("591a710320156761bdf68a06"), "_class" : "mis.domain.PyramidSelling", ... ... "status" : true, "createdDate" : ISODate("2017-05-16T03:24:51.511Z")}
db.getCollection('pyramidSelling').aggregate([ { $project : { _class : { $split: ["$_class", "."] } } }]);
db.getCollection('pyramidSelling').aggregate( [ { $project: { userName: 1, phone: { prefix: { $substr: [ "$phone", 0, 3 ] }, mobile: { $substr: [ "$phone", 3, 11 ] } }, } } ])
select username, sum(balance) as total from users group by member.
db.member.aggregate([{ $group: { _id: "$username", total: { $sum: "$balance" } } }])