经过几个月对MongoDB的使用,记录下对MongoDB经常用的操作,希望能给大家带来一些帮助。
操作符:$gt:大于,$lt:小于,$gte:大于或等于,$lte:小于或等于,$ne:不等于,$eq:等于,$in $nin : in 和 not in,$inc:+=
>cd c:/mongodb/bin >mongo 192.168.180.35:10001/test -u dhs -p dhs //启动MongoDB客户端 >mongo 192.168.180.35:10001 //启动MongoDB客户端 txjRep:PRIMARY>use db2 txjRep:PRIMARY>db.auth('sa','123456') //用户授权 txjRep:PRIMARY>db.auth('user','123') txjRep:PRIMARY>show collections txjRep:PRIMARY>db.system.users.find() //查看数据库用户信息 txjRep:PRIMARY>db.addUser('root','sa') //设置数据库密码 txjRep:PRIMARY>db.getMongo() //查看当前db的链接机器地址 txjRep:PRIMARY>db.stats() //显示当前db状态
db.runCommand({"distinct":"direcexerciseinfo","key":"P_UserId"}).values.length(); db.collection.find({ "field" : { $gt: value } } ); // field > value db.collection.find({"field":{$in:[a,b,c]}}); db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); // value1 < field < value db.topicbasicinfo.find().limit(100); //查询前100条记录数 # 修改指定数据
db.topicbasicinfo.update({"_id":ObjectId("5525f85e033bd91dac28db8f")},{"$set":{"P_ReplyID":ObjectId("5525e7cfb2e68e39fc692ac1")}}); db.Account.find({_id:{"$gte":ObjectId("551bd0627128130630331fe2")}}); //查询_id大于等于的集合 db.Account.remove({"AccountID":1}); //删除AccountID = 1的记录 db.Account.remove({"Age":{$lt:20}}); //DELETE FROM Account WHERE Age < 20 db.Account.drop(); db.Account.remove(); //全部删除 # 统计聚集中字段符合条件的记录条数 SELECT COUNT(UserName) FROM Account db.Account.find({"UserName":{"$exists":true}}).count(); db.Account.find({"Age":{"$gt":20}}).count(); 统计聚集中符合条件的记录条数 db.Account.find().sort({"UserName":1}) -- 1升序 -1降序 db.Account.find({"Age":{"$gt":20}},{"UserName":1,"Email":1}) // 查询聚集中指定列,且Age > 20 db.Account.find({"P_Organizations.P_UserStatus":1}) // 查询子文档 db.Account.find({"P_TopicImageList":{"$size":3}}) // 查询Array中子文档的数量 db.Account.find({ a:{ $size: 1 } } ); db.Account.find( { a : { $exists : true } } ); // 如果存在元素a,就返回 db.Account.find( { a : { $exists : false } } ); // 如果不存在元素a,就返回 # MongoDB索引操作 db.Account.ensureIndex("username":1) //创索引 db.Account.getIndexes() //查看索引 db.Account.dropIndex({"username":1}) //删除索引 db.Account.ensureIndex({"username":1,"age":-1}) // 创建复合索引 # 超大表Account建立索引[ackground 方式] 不锁表 db.Account.ensureIndex({user_id: 1}, {background:true}) # 计算persons中有多少个国家 去重复 db.runCommand({"distinct":"persons","key":"country"}).values # 删除索引 db.runCommand({dropIndexes:"books",index:"name_-1"}) # 模糊查询 Query.Matches("property","/^value/");
# 在已有集合中新增字段 应该先更新实体再新增字段 db.userextinfo.update({ "$isolated" : "true" },{"$set":{"P_IsTeacher":0}}, { "multi" : true }); # 创建唯一索引 db.books.ensureIndex({name:-1},{unique:true}) # 如果有重复数据且要创建唯一索引 踢出重复值 db.books.ensureIndex({name:-1},{unique:true,dropDups:true}) # 如何强制查询使用指定索引,该索引必须存在 db.books.find({name:"en",number:1}).hint({name:-1}) # 创建二维2D索引 db.map.ensureIndex({gis:"2d"},{min:-1,max:201}) # 圆 查询距离[70,180]最近3个点 db.map.find({gis:{$near:[70,180]}},{_id:0,gis:1}).limit(3) db.map.find({gis:{"$within":{$box:[[50,50],[190,190]]}}}) # 分组查询实例 db.userschoolmateinfo.aggregate({$group:{_id:"$P_Version",count:{$sum:1}}}) # 分组查询实例 db.appchannel.aggregate({$group:{_id:"$P_Channel",count:{$sum:1}}}) # 添加用户 db.addUser('root','root') # 删除某个用户 db.system.users.remove({user:"yunfengcheng"}) # MongoDB3.0添加用户 db.createUser({ user: "root", pwd: "root", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] }) db.createUser({user:"root",pwd:"root",roles:[{role:"userAdminAnyDatabase",db:"admin"}]}) # 查看当前用户的权限 db.runCommand({usersInfo:"root",showPrivileges:true}) # 上锁 db.runCommand({fsync:1,lock:1}) # 解锁 db.currentOp() # 数据修复,清除脏数据,当数据写入时突然断电造成脏数据,性能慢 db.repairDatabase() # 分组命令 db.books.aggregate({$limit:50},{$group:{_id:"$UserID", count: {$sum:1}}},{$sort:{count:-1}}); # MongoDB调用存储过程 db.system.js.save({_id:"addNumbers",value:function(x,y){return x+y;}}); db.eval('addNumbers(4,15)'); 地址:http://chenzhou123520.iteye.com/blog/1637462 # 3.2版本创建部分索引 db.restaurants.createIndex({ cuisine: 1, name: 1 },{ partialFilterExpression: { rating: { $gt: 5 } } })
转载于:https://www.cnblogs.com/tangxiaojun/p/4962297.html
