0

I am trying to substitute the "value" in the below snippet using a variable value but no luck.

last_error = posts.update(
    {'permalink':permalink}, 
    { '$inc': 
        { 'comments."value".num_likes': 1 }
    }, 
    upsert=False, 
    manipulate=False, safe=True
)

The variable holds values like 0,1,2,3 etc and needs to update an array based on the array position (Here the array position is determined by the array value).

Not sure how to pass the variable value to the update command. I tried things like {value} ...[value] between the comments and numlikes. Nothing worked.

1 Answer 1

1

Can't you just concatenate it to the string?

last_error = posts.update(
    {'permalink':permalink},
    {'$inc': {'comments.' + str(value) + '.num_likes': 1}},
    upsert=False,
    manipulate=False,
    safe=True)

And here are two other ways to write line 3 above for good measure:

    {'$inc': {'comments.%s.num_likes' % value: 1}},

or:

    {'$inc': {'comments.{value}.num_likes'.format(value=value): 1}},
Sign up to request clarification or add additional context in comments.

1 Comment

Today is my third day learning python. Interesting stuff and appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.