MongoDB: Difference between revisions
(Created page with "Simple Database connect, insert and find <source lang='python'> import pymongo from pymongo import Connection import datetime connection = Connection() myDB = connection['timo...") |
No edit summary |
||
Line 32: | Line 32: | ||
print myDB.collection.find_one({'author':"Timo"}) | print myDB.collection.find_one({'author':"Timo"}) | ||
</source> | |||
HTML file with jQuery connecting to CGI-script | |||
<source lang='html'> | |||
<!DOCTYPE HTML> | |||
<html> | |||
<head> | |||
<title>Untitled</title> | |||
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> | |||
<script> | |||
$(document).ready(function(){ | |||
$(".names").click(function(){ | |||
thisname= $(this).html(); | |||
$.ajax({ | |||
url: "http://pzwart3.wdka.hro.nl/~tklok/cgi-bin/timongo-read.cgi?var1=name="+ thisname}) | |||
.done( | |||
function(data) { | |||
$('<div />', { html: data }).appendTo('body'); | |||
}); | |||
}); | |||
}); | |||
</script> | |||
</head> | |||
<body> | |||
Get age of: | |||
<div class='names'>Timo</div> | |||
<div class='names'>Mike</div> | |||
</body> | |||
</html> | |||
</source> | </source> |
Revision as of 10:15, 31 May 2012
Simple Database connect, insert and find
import pymongo
from pymongo import Connection
import datetime
connection = Connection()
myDB = connection['timo-database']
##WE ALL SHARE THE SAME MONGO INSTNACE SO USE UNIQUE NAMES FOR NOW
post1 = {"author": "Mike", 'age':21,"text": "My first blog post!","tags": ["mongodb", "python", "pymongo"], "date": datetime.datetime.utcnow() }
post2 = {"author": "Timo", 'age':28,"text": "Timos first blog post!","tags": ["db", "python", "pymongo"], "date": datetime.datetime.utcnow() }
post3 = {"author": "Someone", 'age':30, 'place':'Rotterdam'}
collection = myDB.collection
collection.insert(post2)
collection.insert(post1)
collection.insert(post3)
#cursor = myDB.collection.find();
#print cursor[0]
print myDB.collection.find_one({'place':"Rotterdam"})['age']
myDB.collection.update( {'author':'Timo'} , {'$set': {'age':29} } )
print myDB.collection.find_one({'author':"Timo"})
HTML file with jQuery connecting to CGI-script
<!DOCTYPE HTML>
<html>
<head>
<title>Untitled</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".names").click(function(){
thisname= $(this).html();
$.ajax({
url: "http://pzwart3.wdka.hro.nl/~tklok/cgi-bin/timongo-read.cgi?var1=name="+ thisname})
.done(
function(data) {
$('<div />', { html: data }).appendTo('body');
});
});
});
</script>
</head>
<body>
Get age of:
<div class='names'>Timo</div>
<div class='names'>Mike</div>
</body>
</html>