0

i am a beginner in python and mongodb. My database is like this

{ '_id' : 1,
  'identity' : { 
          'first_name' : 'John',
          'last_name' : 'Doe',
          'phone' : '+440823XXX'
         }
   'status' : 'active'
}

I want to return data which phone is empty or doesn't exists in db. I tried

db.collection.find({"identity" : { "phone" : { "$exists": "true", "$ne": "null" }  }})

But it returned <pymongo.cursor.Cursor at 0x7f5da40e8e80>

How the sintax for getting the query which the value is not empty?

5
  • Try : db.collection.find({"identity"."phone" : { "$exists": "true", "$ne": "null" } }}) Commented Dec 8, 2017 at 12:42
  • @Nikhil Fadnis , It said "syntax invalid " on "identity"."phone" Commented Dec 8, 2017 at 12:49
  • @Diyah, check db.collection.find({"identity.phone" { $exists: "true", $ne: "null" } }) Commented Dec 8, 2017 at 12:53
  • Your correction is working, unfortunately it just return <pymongo.cursor.Cursor at 0x7f5da40f6fd0>. what I want here is to return the data Commented Dec 8, 2017 at 12:55
  • @Diyah, I have updated the answer, please take a look on it. Commented Dec 8, 2017 at 13:04

1 Answer 1

1

Please try this code :

db.collection.find({"identity.phone" { $exists: "true", $ne: "null" } })

It will return a cursor, so

cur = db.collection.find({"identity.phone" { $exists: "true", $ne: "null" } })

and you got the data in cur. just loop through it.

for doc in cur:
...

For further reference check here

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

1 Comment

Thank you very much @Kiran ML. this code: "for doc in cur" that i need. This is because of my misunderstanding.

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.