MongoDB 2.6.4版本操做筆記

通過幾個月對MongoDB的使用,記錄下對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狀態
app

    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/");
ui


    # 在已有集合中新增字段 應該先更新實體再新增字段
    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 } } })
spa

相關文章
相關標籤/搜索