MongoDB: Difference between revisions

From XPUB & Lens-Based wiki
(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
 
(5 intermediate revisions by 2 users not shown)
Line 33: Line 33:
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='html4strict'>
<!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?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>
CGI script that returns the AGE of the name passed with the AJAX call
<source lang='python'>
#!/usr/bin/python
import cgitb, cgi
cgitb.enable()
import pymongo
from pymongo import Connection
import datetime
print "Content-type: text/html"
print
connection = Connection()
myDB = connection['timo-database']
##WE ALL SHARE THE SAME MONGO INSTNACE SO USE UNIQUE NAMES FOR NOW
form = cgi.FieldStorage()
which_name = form.getvalue('name')
collection = myDB.collection
print collection.find_one({"author": which_name})['age']
</source>
</source>

Latest revision as of 10:53, 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?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>


CGI script that returns the AGE of the name passed with the AJAX call

#!/usr/bin/python
import cgitb, cgi
cgitb.enable()

import pymongo
from pymongo import Connection
import datetime

print "Content-type: text/html"
print


connection = Connection()
myDB = connection['timo-database']
##WE ALL SHARE THE SAME MONGO INSTNACE SO USE UNIQUE NAMES FOR NOW

form = cgi.FieldStorage()


which_name = form.getvalue('name')

collection = myDB.collection
print collection.find_one({"author": which_name})['age']