Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

So, I'm using mongoDB and my queries return an array of objects. The objects look like this:

   {
        '_id': ObjectId('5ff891bc20cb7d29dc1d836c'),
        'Model': 'X1150',
        'Platform': 'Server',
        '# of CPU Cores': 4,
        '# of Threads': 4
    }

Let's say I store this specific document in the variable doc

Now, I want to be able to compare the 'Platform' attribute with a string. In JavaScript, for example, I would do:

if (doc.Platform == 'Server') return 1;

but it doesn't seems to work in python (maybe because the attribute is a string?). How can I make this comparison correctly?

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
426 views
Welcome To Ask or Share your Answers For Others

1 Answer

The object you say it's returned it seems to be a dictionary , you can check it by type(doc). If that's the case then if (doc['Platform'] == 'Server') return 1; should work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...