0

Objective : I wanted to insert more than one value in a single document.

Following is an example program, were I need to insert value to MongoDB collection's document

for message in mbox:
    stackf = getattachements(message)
    if len(stackf) > 0:
        for i in range(len(stackf)):
            print stackf[i][0]
            print stackf[i][1]
            post = {'sl':i,'From' : message['From'],'To' : message['To'], 'Date' : message['Date'],'Subject' : message['subject'],'Body' : getbody(message),'Attachement' : [{"Originalname" :stackf[i][0],"Exportpath" : stackf[i][1]}]}
    else:
            post = {'sl':i,'From' : message['From'],'To' : message['To'], 'Date' : message['Date'],'Subject' : message['subject'],'Body' : getbody(message)}

but if "stackf" got any return value - this code doesn't write anything.

1
  • Need more details on the problem. Will your two print statements be executed when stackf got non-empty values? Commented Jul 19, 2015 at 6:30

1 Answer 1

1

You seem to be talking about a "list" of "lists" that you want to transform into a "list" of "dict".

So basically

listOfList = [[1,2],[3,4],[5,6]]
map(lambda x: { "OrginalName": x[0], "ExportPath": x[1] }, listOfList )

Produces:

[         
    {'ExportPath': 2, 'OrginalName': 1}, 
    {'ExportPath': 4, 'OrginalName': 3},
    {'ExportPath': 6, 'OrginalName': 5}
]

So you can use that to construct your statement without trying to look as in:

for message in mbox:
    post = { 
       'From' : message['From'],
       'To' : message['To'],
       'Date' : message['Date'],
       'Subject' : message['subject'],
       'Body' : getbody(message)
    }
    stackf = getattachements(message)
    if len(stackf) > 0:
        mapped = map(lambda x: { "OrginalName": x[0], "ExportPath": x[1] }, stackf )
        post['Attachement'] = mapped

    collection.insert_one(post)

Not sure what the "index" values meant to you other than looking up the current "Attachment" values. But this will insert a new document for every "messsage" and put all the "Attachments" in an array of that document matching your new dict structure.

Sign up to request clarification or add additional context in comments.

5 Comments

If I've more than one value in stack, will it work? After finding the attachment exactly at email 153, mongodb is got getting updated
I've declared variable mapped = "" above if condition . Now it works and insert the value to monogdb as expected.
@jOSe Don't forget to mark as Accepted so people know this solved your problem. You also dont need "mapped" outside of the context where you are not actually adding anything to the "Attachement" of post. Which is why I did this. You just followed the example in the wrong way. I never intended to "print" this out of that context, so it's not an error. It was intentional.
while mapped was not declared above if condition. Database was not getting update after finding stackf list. After declaring the variable it got update
@jOSe Nope. The only pupose of "mapped" in this code is to assign 'Attachement' to the post variable when something exists ( ie. len(stackf) ) the only difference in what you listed is calling print outside of the block where that variable is scoped, which is an error since it is out of scope. You didn't even understand how to approach this before you read this, so why are you offerring critique? This solves the problem. You just interpreted it incorrrectly.

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.