mongoDB簡明教程-python(轉)

????????MongoDB是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。他支持的數據結構非常松散,是類似 json的bjson格式,因此可以存儲比較復雜的數據類型。官方網站:http://www.mongodb.org
????????說一些基本安裝:首先安裝python環境這是必須的了吧,具體步驟略。首先安裝mongo環境,我以window為例,從http://www.mongodb.org/downloads下載Windows 32-bit(我的是32位),解壓到C盤并在C盤建一個data/db目錄存放數據文件,從命令行進入到c:/mongodb-win32-i386-1.6.3/bin,然后運行:mongod.ext run,就這樣服務就啟動好了。然后開始安裝mongo的python模塊:到http://pypi.python.org/pypi/pymongo/下載pymongo-1.9.win32-py2.6.exe然后直接運行,安裝完成就可以開始編碼了。
????????語言很簡潔,下面直接上代碼,聰明的你肯定很快就能上手:

mport pymongo
con = pymongo.Connection('localhost', 27017)mydb = con.mydb # new a database
mydb.add_user('test', 'test') # add a user
mydb.authenticate('test', 'test') # check authmuser = mydb.user # new a tablemuser.save({'id':1, 'name':'test'}) # add a recordmuser.insert({'id':2, 'name':'hello'}) # add a record
muser.find_one() # find a recordmuser.find_one({'id':2}) # find a record by querymuser.create_index('id')muser.find().sort('id', pymongo.ASCENDING) # DESCENDING
# muser.drop() delete table
muser.find({'id':1}).count() # get records numbermuser.find({'id':1}).limit(3).skip(2) # start index is 2 limit 3 recordsmuser.remove({'id':1}) # delet records where id = 1muser.update({'id':2}, {'$set':{'name':'haha'}}) # update one recor

下面再貼一些類似非python的api參考:?

mongo –path
db.AddUser(username,password) 添加用戶
db.auth(usrename,password) 設置數據庫連接驗證
db.cloneDataBase(fromhost) 從目標服務器克隆一個數據庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復制數據庫fromdb—源數據庫名稱,todb—目標數據庫名稱,fromhost—源數據庫服務器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創建一個數據集,相當于一個表
db.currentOp() 取消當前庫的當前操作
db.dropDataBase() 刪除當前數據庫
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個數據集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有數據集合的名稱列表
db.getLastError() 返回最后一個錯誤的提示消息
db.getLastErrorObj() 返回最后一個錯誤的對象
db.getMongo() 取得當前服務器的連接對象get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當操作數據庫的名稱
db.getPrevError() 返回上一個錯誤對象
db.getProfilingLevel() ?什么等級
db.getReplicationInfo() ?什么信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當前庫的當前操作
db.printCollectionStats() 返回當前庫的數據集狀態
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當前數據庫是否為共享數據庫
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復當前數據庫
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 關閉當前服務程序
db.version() 返回當前程序的版本信息

db.linlin.find({id:10}) 返回linlin數據集ID=10的數據集
db.linlin.find({id:10}).count() 返回linlin數據集ID=10的數據總數
db.linlin.find({id:10}).limit(2)返回linlin數據集ID=10的數據集從第二條開始的數據集
db.linlin.find({id:10}).skip(8) 返回linlin數據集ID=10的數據集從0到第八條的數據集
db.linlin.find({id:10}).limit(2).skip(8) 返回linlin數據集ID=1=的數據集從第二條到第八條的數據
db.linlin.find({id:10}).sort() 返回linlin數據集ID=10的排序數據集
db.linlin.findOne([query]) 返回符合條件的一條數據
db.linlin.getDB() 返回此數據集所屬的數據庫名稱
db.linlin.getIndexes() 返回些數據集的索引信息
db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})
db.linlin.mapReduce(mayFunction,reduceFunction,
)
db.linlin.remove(query) 在數據集中刪除一條數據
db.linlin.renameCollection(newName) 重命名些數據集名稱
db.linlin.save(obj) 往數據集中插入一條數據
db.linlin.stats() 返回此數據集的狀態
db.linlin.storageSize() 返回此數據集的存儲大小
db.linlin.totalIndexSize() 返回此數據集的索引文件大小
db.linlin.totalSize() 返回些數據集的總大小
db.linlin.update(query,object[,upsert_bool])在此數據集中更新一條數據
db.linlin.validate() 驗證此數據集
db.linlin.getShardVersion() 返回數據集共享版本號
db.linlin.find({‘name’:'foobar’}) select * from linlin where name=’foobar’
db.linlin.find() select * from linlin
db.linlin.find({‘ID’:10}).count() select count(*) from linlin where ID=10
db.linlin.find().skip(10).limit(20) 從查詢結果的第十條開始讀20條數據 select * from linlin limit 10,20 ———-mysql
db.linlin.find({‘ID’:{$in:[25,35,45]}}) select * from linlin where ID in (25,35,45)
db.linlin.find().sort({‘ID’:-1}) select * from linlin order by ID desc
db.linlin.distinct(‘name’,{‘ID’:{$lt:20}}) select distinct(name) from linlin where ID<20
db.linlin.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})
select name,sum(marks) from linlin group by name
db.linlin.find('this.ID<20′,{name:1}) select name from linlin where ID<20
db.linlin.insert({'name':'foobar’,'age':25}) insert into linlin ('name','age’)values('foobar',25)
db.linlin.insert({'name':'foobar’,'age':25,’email’:'cclove2@163.com’})
db.linlin.remove({}) delete * from linlin
db.linlin.remove({'age':20}) delete linlin where age=20
db.linlin.remove({'age':{$lt:20}}) delete linlin where age<20
db.linlin.remove({'age':{$lte:20}}) delete linlin where age<=20
db.linlin.remove({'age':{$gt:20}}) delete linlin where age>20
db.linlin.remove({‘age’:{$gte:20}}) delete linlin where age>=20
db.linlin.remove({‘age’:{$ne:20}}) delete linlin where age!=20
db.linlin.update({‘name’:'foobar’},{‘$set’:{‘age’:36}}) update linlin set age=36 where name=’foobar’
db.linlin.update({‘name’:'foobar’},{‘$inc’:{‘age’:3}}) update linlin set age=age+3 where name=’foobar’

轉載于:https://www.cnblogs.com/roland1982/p/3457544.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/376220.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376220.shtml
英文地址,請注明出處:http://en.pswp.cn/news/376220.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HTTP基礎10--web(2)

因輸出值轉義不完全引發的安全漏洞 實施 Web 應用的安全對策可大致分為以下兩部分。 客戶端的驗證Web 應用端&#xff08;服務器端&#xff09;的驗證: 輸入值驗證 / 輸出值轉義客戶端允許篡改數據或關閉 JavaScript&#xff0c;不適合將 JavaScript 驗證作為安全的防范對策。保…

單一課和綜合課的劃分依據_武夷巖茶產地如何劃分?

產地是指某種物品的生產、出產或加工制造的地點&#xff0c;日常含義是指某種物品的主要生產地。本文探討的武夷巖茶種植產地&#xff0c;也就是當地茶人俗稱的“山場”。武夷巖茶“山場”的俗稱可能緣起于宋代的茶政。宋代官府設置“榷&#xff08;qu&#xff09;茶場”&#…

windows文件路徑大于MAX_PATH

如果文件路徑大于MAX_PATH&#xff0c;是無法直接用CreatFile、fopen等方法來打開文件 但是可以通過在路徑前面加上“\\?\”來獲取文件 比如想要打開下面的文件123.txt&#xff0c;但是文件路徑是很長的&#xff08;假設…是200個字符&#xff09;&#xff1a; C:\123...\1…

C# 枚舉 字符串 轉換

普通方法 這種方法盡管很SB但確實可以解決問題 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){string SelPath "";switch (comboBox1.SelectedIndex){case 0: SelPath System.Environment.GetFolderPath(System.Environment.SpecialFo…

arduino 機器視覺編程_萬物皆可仿真的MATLAB/Simulink神奇在哪?解析如何將其應用于一整套機器人設計開發流程...

MATLAB/Simulink&#xff1a;萬物皆可仿真 MATLAB是由美國MathWorks公司出品的一款商業數學軟件。它是一個多功能的科學計算平臺&#xff0c;將算法開發、數據分析、矩陣計算等諸多強大功能集成在一個易于操作的視窗環境中。MATLAB下的Simulink更是被認為可以“仿真任何系統”。…

排序算法(1) 快速排序 C++實現

快速排序基本特性 時間復雜度&#xff1a;O&#xff08;n*lgn&#xff09;最壞&#xff1a;O&#xff08;n^2&#xff09;空間復雜度&#xff1a;最好情況下&#xff1a;O&#xff08;lgn&#xff09;&#xff0c;最壞情況&#xff1a;O(n)&#xff0c;平均情況&#xff1a;O(l…

boost 變量類型轉換

如果vs版本比較低&#xff0c;會不支持一些std類型轉換函數&#xff08;vs2008就不支持&#xff09;&#xff0c;比如&#xff1a; std::to_string \\數字轉字符串 std::stoll \\字符串轉數字而且項目碰巧用boost庫&#xff0c;可以考慮用下面的的方法來進行類型轉換…

PB增刪改

新建一個數據窗口----選擇需要更新的表&#xff0c;或者直接寫sql也可以如下圖已經建立好的數據窗口&#xff0c;根據要求將需要更新的列、unigue key 還有需要更新的表設置好&#xff0c;【將需要更新列的taborder設置大于0 這樣維護的時候可以編輯&#xff08;等于0是不能編輯…

(五十六)iOS多線程之NSOperation

NSOpertation是一套OC的API&#xff0c;是對GCD進行的Cocoa抽象。 NSOperation有兩種不同類型的隊列&#xff0c;主隊列和自定義隊列。 主隊列運行于主線程上&#xff0c;自定義隊列在后臺運行。 【NSBlockOperation】 通過Block創建任務&#xff0c;下面比較主隊列和自定義隊列…

android 系統源碼調試 局部變量值_如何方便快速的整編Android 9.0系統源碼?

點擊上方“劉望舒”&#xff0c;選擇“星標”多點在看&#xff0c;就是真愛&#xff01;作者 : 劉望舒 | 來源 &#xff1a;劉望舒的博客地址&#xff1a;http://liuwangshu.cn/framework/aosp/3-compiling-aosp.html前言在上一篇文章是時候下載Android 9.0系統源碼了中&…

boost 文件操作

如果要簡單處理文件和文件夾的時候&#xff08;刪除、重命名等&#xff09;&#xff0c;使用Windows的系統函數會十分麻煩&#xff0c;可以嘗試一下使用Boost庫來進行處理 頭文件 #include <boost/filesystem.hpp>如果要獲得每次處理的結果錯誤碼&#xff0c;需要加上頭…

讓“是男人就下到100層”在Android平臺上跑起來

原工程:https://github.com/jeekun/DownFloors 移植后的代碼&#xff1a;HelloCpp.zip 移植后的APK&#xff1a;HelloCpp.apk 說明&#xff1a;&#xff08;cocos2d-x版本是“ 2.2&#xff09; 1.該工程是直接在HelloCpp上修改完成,所以包名也不修改了 2.原工程里面可能是采用g…

Codeforces Round #277 (Div. 2) 題解

Codeforces Round #277 (Div. 2)A. Calculating Functiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFor a positive integer n lets define a function f: f(n)???-?1??2?-?3??..??(?-?1)nn Your …

QT 邊框圓角處理

平時的邊框是平角的&#xff1a; 如果需要圓角的話&#xff0c;就要加stylesheet加上這個&#xff1a; border-radius:3px;比如&#xff1a; QPushButton{ border-radius:3px; }就變成圓角了&#xff1a; px前面的數字越大就越圓&#xff0c;比如5px比3px圓 假如只需要某一…

3級調度 fpga_Vivado HLS學習筆記——1.了解FPGA架構

本篇文章為本人學習Xilinx的Vivado HLS教程記錄的學習筆記&#xff0c;僅供學習參考。Vivado HLS官方視頻教程&#xff1a;優酷視頻?v.youku.com目錄&#xff1a; Vivado HLS課程簡介FPGA與CPU、GPU、DSP的區別FPGA的優勢Xilinx FPGA架構:邏輯單元、算術邏輯單元、存儲單元使用…

[LeetCode]Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思考&#xff1a;DFS。 /*** Definition for binary tree* struct TreeNode {* int val;* Tree…

BZOJ2435 [Noi2011]道路修建

這是NOI11年題&#xff0c;你在逗我&#xff1f; 直接dfs就可以了&#xff0c;Linux下貌似不會爆棧。。。 1 /**************************************************************2 Problem: 24353 User: rausen4 Language: C5 Result: Accepted6 Time:5184 …

Qt異常結束程序無法重新運行

有時候代碼有問題會導致qt異常結束 修改完后重新運行又會出現 查看任務管理器又沒有這個進程 可以使用資源管理器打開看看 也可以考慮使用process explorer查看 發現程序掛起來&#xff0c;結束掉它就可以重新運行了

hadooppythonsql_半小時搞定Hadoop+Mysql+Hive+Python

1. 說明搭建過Hadoop集群的小伙伴一定知道&#xff0c;如果不用docker&#xff0c;半小時配好HadoopMysqlHive(后簡稱Hive)肯定是胡吹&#xff0c;有了Docker鏡像&#xff0c;沒有說明文檔&#xff0c;配好了也不一定會用。本文將介紹如何在半小時內&#xff0c;讓Hive在你的Li…

PHP 切割字符串 點號 不用雙斜杠

$name "tupian.png"; $nameArr explode(".", $name); 習慣了Java的程序員容易寫成 $nameArr explode("\\.", $name);//在PHP中是不正確的轉載于:https://www.cnblogs.com/wuyou/p/3463425.html