时间:2021-07-01 10:21:17 帮助过:16人阅读
ps ajx | grep mongo
表达式:‘$列名‘
db.stu.aggregate([ {$group: { _id:‘$gender‘, counter:{$sum:1} } } ])
db.stu.aggregate([ {$group: { _id:null, counter:{$sum:1}, avgAge:{$avg:‘$age‘} } } ])
db.stu.aggregate([ {$group: { _id:‘$gender‘, name:{$push:‘$name‘} } } ])
db.stu.aggregate([ {$group: { _id:‘$gender‘, name:{$push:‘$$ROOT‘} } } ])
db.stu.aggregate([ {$match:{age:{$gt:20}}} ])
db.stu.aggregate([ {$match:{age:{$gt:20}}}, {$group:{_id:‘$gender‘,counter:{$sum:1}}} ])
db.stu.aggregate([ {$project:{_id:0,name:1,age:1}} ])
db.stu.aggregate([ {$group:{_id:‘$gender‘,counter:{$sum:1}}}, {$project:{_id:0,counter:1}} ])
b.stu.aggregate([{$sort:{age:1}}])
db.stu.aggregate([ {$group:{_id:‘$gender‘,counter:{$sum:1}}}, {$sort:{counter:-1}} ])
db.stu.aggregate([{$limit:2}])
db.stu.aggregate([{$skip:2}])
db.stu.aggregate([ {$group:{_id:‘$gender‘,counter:{$sum:1}}}, {$sort:{counter:1}}, {$skip:1}, {$limit:1} ])
db.集合名称.aggregate([{$unwind:‘$字段名称‘}])
db.t2.insert({_id:1,item:‘t-shirt‘,size:[‘S‘,‘M‘,‘L‘]})
db.t2.aggregate([{$unwind:‘$size‘}])
db.inventory.aggregate([{ $unwind:{ path:‘$字段名称‘, preserveNullAndEmptyArrays:<boolean>#防止数据丢失 } }])
db.t3.insert([ { "_id" : 1, "item" : "a", "size": [ "S", "M", "L"] }, { "_id" : 2, "item" : "b", "size" : [ ] }, { "_id" : 3, "item" : "c", "size": "M" }, { "_id" : 4, "item" : "d" }, { "_id" : 5, "item" : "e", "size" : null } ])
db.t3.aggregate([{$unwind:‘$size‘}])
db.t3.aggregate([{$unwind:{path:‘$sizes‘,preserveNullAndEmptyArrays:true}}])
use admin db.createUser({ user:‘admin‘, pwd:‘123‘, roles:[{role:‘root‘,db:‘admin‘}] })
sudo vi /etc/mongod.conf
security:
authorization: enabled
sudo service mongod stop sudo service mongod start
mongo -u ‘admin‘ -p ‘123‘ --authenticationDatabase ‘admin‘
use test1 show users
db.createUser({ user:‘t1‘, pwd:‘123‘, roles:[{role:‘readWrite‘,db:‘test1‘}] })
mongo -u t1 -p 123 --authenticationDatabase test1
db.updateUser(‘t1‘,{pwd:‘456‘})
mkdir t1 mkdir t2
mongod --bind_ip 192.168.196.128 --port 27017 --dbpath ~/Desktop/t1 --replSet rs0 mongod --bind_ip 192.168.196.128 --port 27018 --dbpath ~/Desktop/t2 --replSet rs0
mongo --host 192.168.196.128 --port 27017
rs.initiate()
rs.status()
rs.add(‘192.168.196.128:27018‘)
mongo --host 192.168.196.128 --port 27018
use test1 for(i=0;i<10;i++){db.t1.insert({_id:i})} db.t1.find()
rs.slaveOk() db.t1.find()
rs.remove(‘192.168.196.128:27018‘)
mongodump -h dbhost -d dbname -o dbdirectory
sudo mkdir test1bak
sudo mongodump -h 192.168.196.128:27017 -d test1 -o ~/Desktop/test1bak
mongorestore -h dbhost -d dbname --dir dbdirectory
mongorestore -h 192.168.196.128:27017 -d test2 --dir ~/Desktop/test1bak/test1
进入虚拟环境
sudo pip install pymongo
或源码安装
python setup.py
import pymongo client=pymongo.MongoClient("localhost", 27017) db=client.test1 stu = db.stu s1={name:‘gj‘,age:18} s1_id = stu.insert_one(s1).inserted_id s2=stu.find_one() for cur in stu.find(): print cur cur=stu.find() cur.next() cur.next() cur.next() print stu.count()
Mongodb的学习整理(下)
标签:sum log tar 技术 透明 first count div god