show dbs show database names
> show dbs local (empty) logging 0.203125GB test 0.203125GB
show collections show collections in current database
> show collections bios system.indexes
另一種用法是show tables
> show tables bios system.indexes
存儲嵌套的對象
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()
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 foo db.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"})
{ "_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", "."] } } } ]);