Adding new item to pymongo cursor












0















I am trying to add a new field to a json string (or to be more exact a bson string) so i can pass this onto the front end.



I have done a load of research, i have tried treating results like a dictionary, i tried adding the field to the database but still nothing.



@app.route('/')
@app.route('/index')
def index():
i=10
make = db.cars.distinct("Make")
model = db.cars.distinct("Short Model")
year = db.cars.distinct("Year introduced")
results = db.cars.find().limit(i)

print(results[0]['Make'])
x=0
while x < i:
print(x)
r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
r=r.json()
print(results[x])
results[x]['carImage'] = r['url']
print(results[x])
x=x+1
print("finished")
return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


existing string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y'}


desired string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y', 'carImage': 'http://images.capnetwork.co.uk/VehicleImage.aspx?SUBID=152231&HASHCODE=FE9015D10FEB72AE9042DB62DAC0ACFE&DB=car&CAPID=74908&VIEWPOINT=3'}


i would like within the json string to have a new field called carImage with the url in it, but i cant get this to work.










share|improve this question

























  • Where exactly are you looking at this string? Is it the print(results[x])?

    – mkrieger1
    Jan 2 at 0:52











  • yes both print(results[x]) are showing the same string.

    – Matt Farrell
    Jan 2 at 0:58











  • What happens if you just print r['url'] without trying to add it to results? What happens if you omit the first print(results[x])? What happens if you add some arbitrary fixed value (like the string 'HELLO') to results, instead of r['url']?

    – mkrieger1
    Jan 2 at 1:00













  • images.capnetwork.co.uk/…

    – Matt Farrell
    Jan 2 at 1:02











  • and with fixed values its the same

    – Matt Farrell
    Jan 2 at 1:02
















0















I am trying to add a new field to a json string (or to be more exact a bson string) so i can pass this onto the front end.



I have done a load of research, i have tried treating results like a dictionary, i tried adding the field to the database but still nothing.



@app.route('/')
@app.route('/index')
def index():
i=10
make = db.cars.distinct("Make")
model = db.cars.distinct("Short Model")
year = db.cars.distinct("Year introduced")
results = db.cars.find().limit(i)

print(results[0]['Make'])
x=0
while x < i:
print(x)
r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
r=r.json()
print(results[x])
results[x]['carImage'] = r['url']
print(results[x])
x=x+1
print("finished")
return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


existing string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y'}


desired string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y', 'carImage': 'http://images.capnetwork.co.uk/VehicleImage.aspx?SUBID=152231&HASHCODE=FE9015D10FEB72AE9042DB62DAC0ACFE&DB=car&CAPID=74908&VIEWPOINT=3'}


i would like within the json string to have a new field called carImage with the url in it, but i cant get this to work.










share|improve this question

























  • Where exactly are you looking at this string? Is it the print(results[x])?

    – mkrieger1
    Jan 2 at 0:52











  • yes both print(results[x]) are showing the same string.

    – Matt Farrell
    Jan 2 at 0:58











  • What happens if you just print r['url'] without trying to add it to results? What happens if you omit the first print(results[x])? What happens if you add some arbitrary fixed value (like the string 'HELLO') to results, instead of r['url']?

    – mkrieger1
    Jan 2 at 1:00













  • images.capnetwork.co.uk/…

    – Matt Farrell
    Jan 2 at 1:02











  • and with fixed values its the same

    – Matt Farrell
    Jan 2 at 1:02














0












0








0








I am trying to add a new field to a json string (or to be more exact a bson string) so i can pass this onto the front end.



I have done a load of research, i have tried treating results like a dictionary, i tried adding the field to the database but still nothing.



@app.route('/')
@app.route('/index')
def index():
i=10
make = db.cars.distinct("Make")
model = db.cars.distinct("Short Model")
year = db.cars.distinct("Year introduced")
results = db.cars.find().limit(i)

print(results[0]['Make'])
x=0
while x < i:
print(x)
r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
r=r.json()
print(results[x])
results[x]['carImage'] = r['url']
print(results[x])
x=x+1
print("finished")
return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


existing string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y'}


desired string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y', 'carImage': 'http://images.capnetwork.co.uk/VehicleImage.aspx?SUBID=152231&HASHCODE=FE9015D10FEB72AE9042DB62DAC0ACFE&DB=car&CAPID=74908&VIEWPOINT=3'}


i would like within the json string to have a new field called carImage with the url in it, but i cant get this to work.










share|improve this question
















I am trying to add a new field to a json string (or to be more exact a bson string) so i can pass this onto the front end.



I have done a load of research, i have tried treating results like a dictionary, i tried adding the field to the database but still nothing.



@app.route('/')
@app.route('/index')
def index():
i=10
make = db.cars.distinct("Make")
model = db.cars.distinct("Short Model")
year = db.cars.distinct("Year introduced")
results = db.cars.find().limit(i)

print(results[0]['Make'])
x=0
while x < i:
print(x)
r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
r=r.json()
print(results[x])
results[x]['carImage'] = r['url']
print(results[x])
x=x+1
print("finished")
return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


existing string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y'}


desired string



{'_id': ObjectId('5c2bebbe7b94a040bce5eadf'), 'Vehicle ID': '74908', 'Make': 'MERCEDES-BENZ', 'Short Model': 'A CLASS', 'Long Model': 'A CLASS HATCHBACK', 'Trim': 'SE', 'Derivative': 'A160 SE 5dr Auto', 'Year introduced': '2016', 'Year discontinued': '', 'Currently Available': 'Y', 'carImage': 'http://images.capnetwork.co.uk/VehicleImage.aspx?SUBID=152231&HASHCODE=FE9015D10FEB72AE9042DB62DAC0ACFE&DB=car&CAPID=74908&VIEWPOINT=3'}


i would like within the json string to have a new field called carImage with the url in it, but i cant get this to work.







python mongodb pymongo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 1:13









mkrieger1

4,66522033




4,66522033










asked Jan 2 at 0:19









Matt FarrellMatt Farrell

77118




77118













  • Where exactly are you looking at this string? Is it the print(results[x])?

    – mkrieger1
    Jan 2 at 0:52











  • yes both print(results[x]) are showing the same string.

    – Matt Farrell
    Jan 2 at 0:58











  • What happens if you just print r['url'] without trying to add it to results? What happens if you omit the first print(results[x])? What happens if you add some arbitrary fixed value (like the string 'HELLO') to results, instead of r['url']?

    – mkrieger1
    Jan 2 at 1:00













  • images.capnetwork.co.uk/…

    – Matt Farrell
    Jan 2 at 1:02











  • and with fixed values its the same

    – Matt Farrell
    Jan 2 at 1:02



















  • Where exactly are you looking at this string? Is it the print(results[x])?

    – mkrieger1
    Jan 2 at 0:52











  • yes both print(results[x]) are showing the same string.

    – Matt Farrell
    Jan 2 at 0:58











  • What happens if you just print r['url'] without trying to add it to results? What happens if you omit the first print(results[x])? What happens if you add some arbitrary fixed value (like the string 'HELLO') to results, instead of r['url']?

    – mkrieger1
    Jan 2 at 1:00













  • images.capnetwork.co.uk/…

    – Matt Farrell
    Jan 2 at 1:02











  • and with fixed values its the same

    – Matt Farrell
    Jan 2 at 1:02

















Where exactly are you looking at this string? Is it the print(results[x])?

– mkrieger1
Jan 2 at 0:52





Where exactly are you looking at this string? Is it the print(results[x])?

– mkrieger1
Jan 2 at 0:52













yes both print(results[x]) are showing the same string.

– Matt Farrell
Jan 2 at 0:58





yes both print(results[x]) are showing the same string.

– Matt Farrell
Jan 2 at 0:58













What happens if you just print r['url'] without trying to add it to results? What happens if you omit the first print(results[x])? What happens if you add some arbitrary fixed value (like the string 'HELLO') to results, instead of r['url']?

– mkrieger1
Jan 2 at 1:00







What happens if you just print r['url'] without trying to add it to results? What happens if you omit the first print(results[x])? What happens if you add some arbitrary fixed value (like the string 'HELLO') to results, instead of r['url']?

– mkrieger1
Jan 2 at 1:00















images.capnetwork.co.uk/…

– Matt Farrell
Jan 2 at 1:02





images.capnetwork.co.uk/…

– Matt Farrell
Jan 2 at 1:02













and with fixed values its the same

– Matt Farrell
Jan 2 at 1:02





and with fixed values its the same

– Matt Farrell
Jan 2 at 1:02












1 Answer
1






active

oldest

votes


















0















  • I think the reason why your changes are not reflecting, is because
    you are trying to access the documents inside the cursor using an
    index (like a list), but that doesn't seem to work.

  • In your last statement, you are trying to return the cursor object directly in the render_template function which is wrong. The cursor object is not a list (python list) of json documents as you are assuming.


The below code will work if you want to just see the changes being reflected.



        @app.route('/')
@app.route('/index')
def index():
i=10
make = db.cars.distinct("Make")
model = db.cars.distinct("Short Model")
year = db.cars.distinct("Year introduced")
results = db.cars.find().limit(i)

#print(results[0]['Make'])

for doc in results:
r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
r=r.json()
print(doc)
doc['carImage'] = r['url']
print(doc)
print("finished")
# return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


If you want to update all documents and return, then convert result of find() to a list as below. But, do note that, it will load all documents into the memory.



@app.route('/')
@app.route('/index')
def index():
i=10
make = db.cars.distinct("Make")
model = db.cars.distinct("Short Model")
year = db.cars.distinct("Year introduced")
results = list(db.cars.find().limit(i)) #Note that here we convert the result to a list

#print(results[0]['Make'])

for doc in results:
r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
r=r.json()
print(doc)
doc['carImage'] = r['url']
print(doc)
print("finished")
return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999996%2fadding-new-item-to-pymongo-cursor%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    • I think the reason why your changes are not reflecting, is because
      you are trying to access the documents inside the cursor using an
      index (like a list), but that doesn't seem to work.

    • In your last statement, you are trying to return the cursor object directly in the render_template function which is wrong. The cursor object is not a list (python list) of json documents as you are assuming.


    The below code will work if you want to just see the changes being reflected.



            @app.route('/')
    @app.route('/index')
    def index():
    i=10
    make = db.cars.distinct("Make")
    model = db.cars.distinct("Short Model")
    year = db.cars.distinct("Year introduced")
    results = db.cars.find().limit(i)

    #print(results[0]['Make'])

    for doc in results:
    r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
    r=r.json()
    print(doc)
    doc['carImage'] = r['url']
    print(doc)
    print("finished")
    # return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


    If you want to update all documents and return, then convert result of find() to a list as below. But, do note that, it will load all documents into the memory.



    @app.route('/')
    @app.route('/index')
    def index():
    i=10
    make = db.cars.distinct("Make")
    model = db.cars.distinct("Short Model")
    year = db.cars.distinct("Year introduced")
    results = list(db.cars.find().limit(i)) #Note that here we convert the result to a list

    #print(results[0]['Make'])

    for doc in results:
    r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
    r=r.json()
    print(doc)
    doc['carImage'] = r['url']
    print(doc)
    print("finished")
    return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)





    share|improve this answer




























      0















      • I think the reason why your changes are not reflecting, is because
        you are trying to access the documents inside the cursor using an
        index (like a list), but that doesn't seem to work.

      • In your last statement, you are trying to return the cursor object directly in the render_template function which is wrong. The cursor object is not a list (python list) of json documents as you are assuming.


      The below code will work if you want to just see the changes being reflected.



              @app.route('/')
      @app.route('/index')
      def index():
      i=10
      make = db.cars.distinct("Make")
      model = db.cars.distinct("Short Model")
      year = db.cars.distinct("Year introduced")
      results = db.cars.find().limit(i)

      #print(results[0]['Make'])

      for doc in results:
      r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
      r=r.json()
      print(doc)
      doc['carImage'] = r['url']
      print(doc)
      print("finished")
      # return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


      If you want to update all documents and return, then convert result of find() to a list as below. But, do note that, it will load all documents into the memory.



      @app.route('/')
      @app.route('/index')
      def index():
      i=10
      make = db.cars.distinct("Make")
      model = db.cars.distinct("Short Model")
      year = db.cars.distinct("Year introduced")
      results = list(db.cars.find().limit(i)) #Note that here we convert the result to a list

      #print(results[0]['Make'])

      for doc in results:
      r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
      r=r.json()
      print(doc)
      doc['carImage'] = r['url']
      print(doc)
      print("finished")
      return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)





      share|improve this answer


























        0












        0








        0








        • I think the reason why your changes are not reflecting, is because
          you are trying to access the documents inside the cursor using an
          index (like a list), but that doesn't seem to work.

        • In your last statement, you are trying to return the cursor object directly in the render_template function which is wrong. The cursor object is not a list (python list) of json documents as you are assuming.


        The below code will work if you want to just see the changes being reflected.



                @app.route('/')
        @app.route('/index')
        def index():
        i=10
        make = db.cars.distinct("Make")
        model = db.cars.distinct("Short Model")
        year = db.cars.distinct("Year introduced")
        results = db.cars.find().limit(i)

        #print(results[0]['Make'])

        for doc in results:
        r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
        r=r.json()
        print(doc)
        doc['carImage'] = r['url']
        print(doc)
        print("finished")
        # return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


        If you want to update all documents and return, then convert result of find() to a list as below. But, do note that, it will load all documents into the memory.



        @app.route('/')
        @app.route('/index')
        def index():
        i=10
        make = db.cars.distinct("Make")
        model = db.cars.distinct("Short Model")
        year = db.cars.distinct("Year introduced")
        results = list(db.cars.find().limit(i)) #Note that here we convert the result to a list

        #print(results[0]['Make'])

        for doc in results:
        r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
        r=r.json()
        print(doc)
        doc['carImage'] = r['url']
        print(doc)
        print("finished")
        return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)





        share|improve this answer














        • I think the reason why your changes are not reflecting, is because
          you are trying to access the documents inside the cursor using an
          index (like a list), but that doesn't seem to work.

        • In your last statement, you are trying to return the cursor object directly in the render_template function which is wrong. The cursor object is not a list (python list) of json documents as you are assuming.


        The below code will work if you want to just see the changes being reflected.



                @app.route('/')
        @app.route('/index')
        def index():
        i=10
        make = db.cars.distinct("Make")
        model = db.cars.distinct("Short Model")
        year = db.cars.distinct("Year introduced")
        results = db.cars.find().limit(i)

        #print(results[0]['Make'])

        for doc in results:
        r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
        r=r.json()
        print(doc)
        doc['carImage'] = r['url']
        print(doc)
        print("finished")
        # return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)


        If you want to update all documents and return, then convert result of find() to a list as below. But, do note that, it will load all documents into the memory.



        @app.route('/')
        @app.route('/index')
        def index():
        i=10
        make = db.cars.distinct("Make")
        model = db.cars.distinct("Short Model")
        year = db.cars.distinct("Year introduced")
        results = list(db.cars.find().limit(i)) #Note that here we convert the result to a list

        #print(results[0]['Make'])

        for doc in results:
        r = requests.get('https://izrite.com:5555/image/' + results[x]['Vehicle ID'])
        r=r.json()
        print(doc)
        doc['carImage'] = r['url']
        print(doc)
        print("finished")
        return render_template('index.html', title='Home', numresults=str(i), make=make, model=model, year=year, results=results)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 13:17









        JayJay

        15.3k2165122




        15.3k2165122
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999996%2fadding-new-item-to-pymongo-cursor%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter