librelist archives

« back to archive

Flask-PyMongo inserting into DB

Flask-PyMongo inserting into DB

From:
pronoyc@gmail.com
Date:
2012-05-14 @ 08:47
Hello, can anyone give me a small example on how to insert data into a
Mongo Collection?

The Flask-PyMonogo examples don't seem to contain this particular bit. Only
retrieval is explained.


Pronoy Chopra

Re: [flask] Flask-PyMongo inserting into DB

From:
Simon Sapin
Date:
2012-05-14 @ 09:50
Le 14/05/2012 10:47, pronoyc@gmail.com a écrit :
> The Flask-PyMonogo examples don't seem to contain this particular bit.
> Only retrieval is explained.

Hi,

I don’t use MongoDB but Flask-XYZ is generally often a thin wrapper 
around XYZ. Have a look at the PyMongo documentation.

-- 
Simon Sapin

Re: [flask] Flask-PyMongo inserting into DB

From:
dorian i
Date:
2012-05-16 @ 21:53
Here you can look at code that i used for some testing, it should be
working:


from time import strftime
from flask import Flask
#from flask.ext.pymongo import PyMongo
import datetime
import pygeoip
from pymongo import Connection
app = Flask(__name__)
geoip = pygeoip.GeoIP('GeoLiteCity.dat')

connection = Connection('localhost', 27017)
db = connection.test
foo = db.foo
acc = db.acc

def daterange(start_date, end_date):
    for n in range((end_date - start_date).days):
        yield start_date + datetime.timedelta(n)

@app.route('/')
def hello_world():
    gg = 'g'
    hh = 0
    #data = db.foo.find({},{"stats.2012.1.sum":1})#get top videos for
month/year
    data = db.foo.find({"vid":"5"},{"geo.sum":1})
    #db.foo.update({"vid":"5"},{"$unset":{"geo.2012.1":1}})
    for d in data:
        gg += '%s' % d
    return "%s"%gg

@app.route('/view/<int:acc_id>/<int:video_id>/')
def view_video(video_id,acc_id):
    f=0
    gg = datetime.date.today()
    vid = "%s" % video_id
    aid = "%s" % acc_id
    myip = "31.44.76.73"
    co = geoip.country_code_by_addr(myip)
    do = 'domain:com'
    thisday = 'stats.%s.%s.%s.sum.views' % (gg.year,gg.month,gg.day)
    thismonth = "stats.%s.%s.sum.views"%(gg.year,gg.month)
    thisyear = "stats.%s.sum.views"%gg.year
    alltime = "stats.sum.views"
    geomonth = "geo.%s.%s.sum.%s.sum.views"%(gg.year,gg.month,co)
    geoyear = "geo.%s.sum.%s.sum.views"%(gg.year,co)
    geotime = "geo.sum.%s.sum.views"%co


#   Add platform(windows,iphone) and browser
    where = {"vid":vid,"acc":aid}
    what = {"$inc":{
        alltime:1,thisyear:1,thismonth:1,thisday:1,
#        geotime:1,geoyear:1,geomonth:1,
        }}

    #update embed,
    #acc.update({"acc":aid},{"$inc":{"vs":1,what:1}},True)
    while True:
        foo.update(where,what,True)
        f+=1
        if f>0:
            break


    return 'day %s,month %s,year %s and video id %s and country is %s' %
(gg.day,gg.month,gg.year,video_id,co)
@app.route('/st/')
def view_video():
#   Add platform(windows,iphone) and browser
    video_id=1
    acc_id=1


    #update embed,
    #acc.update({"acc":aid},{"$inc":{"vs":1,what:1}},True)
    for gg in daterange(datetime.date(2010,1,1), datetime.date(2011,1,1)):
        vid = "%s" % video_id
        aid = "%s" % acc_id
        thisday = 'stats.%s.%s.%s.sum.views' % (gg.year,gg.month,gg.day)
        thismonth = "stats.%s.%s.sum.views"%(gg.year,gg.month)
        thisyear = "stats.%s.sum.views"%gg.year
        alltime = "stats.sum.views"
        thisday1 = 'stats.%s.%s.%s.sum.start' % (gg.year,gg.month,gg.day)
        thismonth1 = "stats.%s.%s.sum.start"%(gg.year,gg.month)
        thisyear1 = "stats.%s.sum.start"%gg.year
        alltime1 = "stats.sum.start"
        thisday2 = 'stats.%s.%s.%s.sum.sec' % (gg.year,gg.month,gg.day)
        thismonth2 = "stats.%s.%s.sum.sec"%(gg.year,gg.month)
        thisyear2 = "stats.%s.sum.sec"%gg.year
        alltime2 = "stats.sum.sec"
        thisday3 = 'stats.%s.%s.%s.sum.fin' % (gg.year,gg.month,gg.day)
        thismonth3 = "stats.%s.%s.sum.fin"%(gg.year,gg.month)
        thisyear3 = "stats.%s.sum.fin"%gg.year
        alltime3 = "stats.sum.fin"
        where = {"vid":vid,"acc":aid}
        what =

{"$inc":{alltime:1,thisyear:1,thismonth:1,thisday:1,alltime1:1,thisyear1:1,thismonth1:1,thisday1:1,
                        alltime2:1,thisyear2:1,thismonth2:1,thisday2:1,
                        alltime3:1,thisyear3:1,thismonth3:1,thisday3:1}}
        foo.update(where,what,True)

    return 'd'

if __name__ == '__main__':
    app.run(debug=True)





On Mon, May 14, 2012 at 11:50 AM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 14/05/2012 10:47, pronoyc@gmail.com a écrit :
> > The Flask-PyMonogo examples don't seem to contain this particular bit.
> > Only retrieval is explained.
>
> Hi,
>
> I don’t use MongoDB but Flask-XYZ is generally often a thin wrapper
> around XYZ. Have a look at the PyMongo documentation.
>
> --
> Simon Sapin
>

Re: [flask] Flask-PyMongo inserting into DB

From:
Dan Crosta
Date:
2012-05-16 @ 21:58
What error are you seeing, or what are you expecting to happen that's not 
happening?

- Dan

On May 16, 2012, at 5:53 PM, dorian i wrote:

> Here you can look at code that i used for some testing, it should be working:
> 
> 
> from time import strftime
> from flask import Flask
> #from flask.ext.pymongo import PyMongo
> import datetime
> import pygeoip
> from pymongo import Connection
> app = Flask(__name__)
> geoip = pygeoip.GeoIP('GeoLiteCity.dat')
> 
> connection = Connection('localhost', 27017)
> db = connection.test
> foo = db.foo
> acc = db.acc
> 
> def daterange(start_date, end_date):
>     for n in range((end_date - start_date).days):
>         yield start_date + datetime.timedelta(n)
> 
> @app.route('/')
> def hello_world():
>     gg = 'g'
>     hh = 0
>     #data = db.foo.find({},{"stats.2012.1.sum":1})#get top videos for month/year
>     data = db.foo.find({"vid":"5"},{"geo.sum":1})
>     #db.foo.update({"vid":"5"},{"$unset":{"geo.2012.1":1}})
>     for d in data:
>         gg += '%s' % d
>     return "%s"%gg
> 
> @app.route('/view/<int:acc_id>/<int:video_id>/')
> def view_video(video_id,acc_id):
>     f=0
>     gg = datetime.date.today()
>     vid = "%s" % video_id
>     aid = "%s" % acc_id
>     myip = "31.44.76.73"
>     co = geoip.country_code_by_addr(myip)
>     do = 'domain:com'
>     thisday = 'stats.%s.%s.%s.sum.views' % (gg.year,gg.month,gg.day)
>     thismonth = "stats.%s.%s.sum.views"%(gg.year,gg.month)
>     thisyear = "stats.%s.sum.views"%gg.year
>     alltime = "stats.sum.views"
>     geomonth = "geo.%s.%s.sum.%s.sum.views"%(gg.year,gg.month,co)
>     geoyear = "geo.%s.sum.%s.sum.views"%(gg.year,co)
>     geotime = "geo.sum.%s.sum.views"%co
> 
> 
> #   Add platform(windows,iphone) and browser
>     where = {"vid":vid,"acc":aid}
>     what = {"$inc":{
>         alltime:1,thisyear:1,thismonth:1,thisday:1,
> #        geotime:1,geoyear:1,geomonth:1,
>         }}
> 
>     #update embed,
>     #acc.update({"acc":aid},{"$inc":{"vs":1,what:1}},True)
>     while True:
>         foo.update(where,what,True)
>         f+=1
>         if f>0:
>             break
> 
> 
>     return 'day %s,month %s,year %s and video id %s and country is %s' %
(gg.day,gg.month,gg.year,video_id,co)
> @app.route('/st/')
> def view_video():
> #   Add platform(windows,iphone) and browser
>     video_id=1
>     acc_id=1
> 
> 
>     #update embed,
>     #acc.update({"acc":aid},{"$inc":{"vs":1,what:1}},True)
>     for gg in daterange(datetime.date(2010,1,1), datetime.date(2011,1,1)):
>         vid = "%s" % video_id
>         aid = "%s" % acc_id
>         thisday = 'stats.%s.%s.%s.sum.views' % (gg.year,gg.month,gg.day)
>         thismonth = "stats.%s.%s.sum.views"%(gg.year,gg.month)
>         thisyear = "stats.%s.sum.views"%gg.year
>         alltime = "stats.sum.views"
>         thisday1 = 'stats.%s.%s.%s.sum.start' % (gg.year,gg.month,gg.day)
>         thismonth1 = "stats.%s.%s.sum.start"%(gg.year,gg.month)
>         thisyear1 = "stats.%s.sum.start"%gg.year
>         alltime1 = "stats.sum.start"
>         thisday2 = 'stats.%s.%s.%s.sum.sec' % (gg.year,gg.month,gg.day)
>         thismonth2 = "stats.%s.%s.sum.sec"%(gg.year,gg.month)
>         thisyear2 = "stats.%s.sum.sec"%gg.year
>         alltime2 = "stats.sum.sec"
>         thisday3 = 'stats.%s.%s.%s.sum.fin' % (gg.year,gg.month,gg.day)
>         thismonth3 = "stats.%s.%s.sum.fin"%(gg.year,gg.month)
>         thisyear3 = "stats.%s.sum.fin"%gg.year
>         alltime3 = "stats.sum.fin"
>         where = {"vid":vid,"acc":aid}
>         what = 
{"$inc":{alltime:1,thisyear:1,thismonth:1,thisday:1,alltime1:1,thisyear1:1,thismonth1:1,thisday1:1,
>                         alltime2:1,thisyear2:1,thismonth2:1,thisday2:1,
>                         alltime3:1,thisyear3:1,thismonth3:1,thisday3:1}}
>         foo.update(where,what,True)
> 
>     return 'd'
> 
> if __name__ == '__main__':
>     app.run(debug=True)
> 
> 
> 
> 
> 
> On Mon, May 14, 2012 at 11:50 AM, Simon Sapin <simon.sapin@exyr.org> wrote:
> Le 14/05/2012 10:47, pronoyc@gmail.com a écrit :
> > The Flask-PyMonogo examples don't seem to contain this particular bit.
> > Only retrieval is explained.
> 
> Hi,
> 
> I don’t use MongoDB but Flask-XYZ is generally often a thin wrapper
> around XYZ. Have a look at the PyMongo documentation.
> 
> --
> Simon Sapin
> 

Re: [flask] Flask-PyMongo inserting into DB

From:
Pronoy Chopra
Date:
2012-05-17 @ 11:09
Using PyMongo directly isn't the problem. Infact if I am using that to 
insert data I can very well use it retrieve data. Flask-Pymongo on the 
other hand doesn't let you enter data or I am doing it wrong. So, yes I 
think I'll be using Pymongo directly.

> Dan Crosta <mailto:dcrosta@late.am>
> 17 May 2012 3:28 AM
> What error are you seeing, or what are you expecting to happen that's 
> not happening?
>
> - Dan
>
>
>
> dorian i <mailto:dorian.hoxha@gmail.com>
> 17 May 2012 3:23 AM
> Here you can look at code that i used for some testing, it should be 
> working:
>
>
> from time import strftime
> from flask import Flask
> #from flask.ext.pymongo import PyMongo
> import datetime
> import pygeoip
> from pymongo import Connection
> app = Flask(__name__)
> geoip = pygeoip.GeoIP('GeoLiteCity.dat')
>
> connection = Connection('localhost', 27017)
> db = connection.test
> foo = db.foo
> acc = db.acc
>
> def daterange(start_date, end_date):
>     for n in range((end_date - start_date).days):
>         yield start_date + datetime.timedelta(n)
>
> @app.route('/')
> def hello_world():
>     gg = 'g'
>     hh = 0
>     #data = db.foo.find({},{"stats.2012.1.sum":1})#get top videos for 
> month/year
>     data = db.foo.find({"vid":"5"},{"geo.sum":1})
>     #db.foo.update({"vid":"5"},{"$unset":{"geo.2012.1":1}})
>     for d in data:
>         gg += '%s' % d
>     return "%s"%gg
>
> @app.route('/view/<int:acc_id>/<int:video_id>/')
> def view_video(video_id,acc_id):
>     f=0
>     gg = datetime.date.today()
>     vid = "%s" % video_id
>     aid = "%s" % acc_id
>     myip = "31.44.76.73"
>     co = geoip.country_code_by_addr(myip)
>     do = 'domain:com'
>     thisday = 'stats.%s.%s.%s.sum.views' % (gg.year,gg.month,gg.day)
>     thismonth = "stats.%s.%s.sum.views"%(gg.year,gg.month)
>     thisyear = "stats.%s.sum.views"%gg.year
>     alltime = "stats.sum.views"
>     geomonth = "geo.%s.%s.sum.%s.sum.views"%(gg.year,gg.month,co)
>     geoyear = "geo.%s.sum.%s.sum.views"%(gg.year,co)
>     geotime = "geo.sum.%s.sum.views"%co
>
>
> #   Add platform(windows,iphone) and browser
>     where = {"vid":vid,"acc":aid}
>     what = {"$inc":{
>         alltime:1,thisyear:1,thismonth:1,thisday:1,
> #        geotime:1,geoyear:1,geomonth:1,
>         }}
>
>     #update embed,
>     #acc.update({"acc":aid},{"$inc":{"vs":1,what:1}},True)
>     while True:
>         foo.update(where,what,True)
>         f+=1
>         if f>0:
>             break
>
>
>     return 'day %s,month %s,year %s and video id %s and country is %s' 
> % (gg.day,gg.month,gg.year,video_id,co)
> @app.route('/st/')
> def view_video():
> #   Add platform(windows,iphone) and browser
>     video_id=1
>     acc_id=1
>
>
>     #update embed,
>     #acc.update({"acc":aid},{"$inc":{"vs":1,what:1}},True)
>     for gg in daterange(datetime.date(2010,1,1), datetime.date(2011,1,1)):
>         vid = "%s" % video_id
>         aid = "%s" % acc_id
>         thisday = 'stats.%s.%s.%s.sum.views' % (gg.year,gg.month,gg.day)
>         thismonth = "stats.%s.%s.sum.views"%(gg.year,gg.month)
>         thisyear = "stats.%s.sum.views"%gg.year
>         alltime = "stats.sum.views"
>         thisday1 = 'stats.%s.%s.%s.sum.start' % (gg.year,gg.month,gg.day)
>         thismonth1 = "stats.%s.%s.sum.start"%(gg.year,gg.month)
>         thisyear1 = "stats.%s.sum.start"%gg.year
>         alltime1 = "stats.sum.start"
>         thisday2 = 'stats.%s.%s.%s.sum.sec' % (gg.year,gg.month,gg.day)
>         thismonth2 = "stats.%s.%s.sum.sec"%(gg.year,gg.month)
>         thisyear2 = "stats.%s.sum.sec"%gg.year
>         alltime2 = "stats.sum.sec"
>         thisday3 = 'stats.%s.%s.%s.sum.fin' % (gg.year,gg.month,gg.day)
>         thismonth3 = "stats.%s.%s.sum.fin"%(gg.year,gg.month)
>         thisyear3 = "stats.%s.sum.fin"%gg.year
>         alltime3 = "stats.sum.fin"
>         where = {"vid":vid,"acc":aid}
>         what = 
> 
{"$inc":{alltime:1,thisyear:1,thismonth:1,thisday:1,alltime1:1,thisyear1:1,thismonth1:1,thisday1:1,
>                         alltime2:1,thisyear2:1,thismonth2:1,thisday2:1,
>                         alltime3:1,thisyear3:1,thismonth3:1,thisday3:1}}
>         foo.update(where,what,True)
>
>     return 'd'
>
> if __name__ == '__main__':
>     app.run(debug=True)
>
>
>
>
>
>
> Simon Sapin <mailto:simon.sapin@exyr.org>
> 14 May 2012 3:20 PM
>
> Hi,
>
> I don’t use MongoDB but Flask-XYZ is generally often a thin wrapper
> around XYZ. Have a look at the PyMongo documentation.
>
> pronoyc@gmail.com <mailto:pronoyc@gmail.com>
> 14 May 2012 2:17 PM
> Hello, can anyone give me a small example on how to insert data into a 
> Mongo Collection?
>
> The Flask-PyMonogo examples don't seem to contain this particular bit. 
> Only retrieval is explained.
>
>
> Pronoy Chopra