0

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!

1
  • 1
    This isn't Javascript - dict keys are not attributes, and indexing is not the same as dot notation. Commented Jan 8, 2021 at 19:30

1 Answer 1

1

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

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

1 Comment

That's exactly what I was looking for. It's a dictionary indeed and the syntax works perfectly. Thank you George!

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.