Milica Pavlovic
Milica Pavlovic

Reputation: 109

Implement MongoDB query in Python

i have this query and it is working in MongoDB Booster, but i don't understand why i can't only copy this in Python, as i do for query on MySQL, and what should I do with this query, so it can work normally in MongoDB

Query that works in MongoDB Booster but it doesn't work when i copy in Python looks like this:

db.bol_rac.aggregate( [
  {
    $project:
      {
        "id" : 1,
        "id_drz": 1,    
        "location" :
        {          
             $cond: { if: { $eq: [ "$id_drzavljanstvo", 688 ] }, then: "Country", else: "Foreign country" }          
         }
      }
   }
] );

Upvotes: 0

Views: 43

Answers (1)

Milica Pavlovic
Milica Pavlovic

Reputation: 109

i mean on this, i have success to solve the problem, problem is that i haven't add '' on if,else,then ..

  from pymongo import MongoClient
    client = MongoClient()

    db = client.bol_rac

    cursor = db.bol_rac.aggregate([
      {
        '$project':
          {
            'id' : 1,
            'id_drz': 1,    
            'location' :
            {          
                 '$cond': { 'if': { '$eq': [ '$id_drz', 688 ] }, 'then': 'Country', 'else': 'Foreign country' }          
             }
          }
       }
    ])

    for document in cursor:
        print(document)

Upvotes: 1

Related Questions