1

i have been trying to query using .py file to mongoDB using pymongo.

So the problem now is that i want the user to input the searchField personally.

This is my code...

from pymongo import Connection

if __name__ == "__main__":
    con = Connection()
    db = con.fyptwitter
    collection = db.fyp


    userInputFirst = str(raw_input("Enter: "))
    userInputSecond = str(raw_input("Enter 2nd: "))
    strFirst =  userInputFirst 

    stringTest = "/" + userInputSecond + "/i"

    query = db.fyp.find({ userInputFirst : stringTest})
    print query
    for i in query:
        print i
        break

from my mongoConnection, it shows that the query is correct. But the problem is that it does not bring up any results.

Any reason why?

  • A couple of things: 1. you don't need the `str()` around `raw_input()` because the result is already a string. 2. You don't need the `break` in the loop - this will terminate the loop after 1 iteration (not sure if that's the cause of your problem) – mhawke Jul 04 '14 at 08:31
  • Sorry about that, i have removed it and still it doesn't work. Reasons being i use break is because my data is big.. thus i just wanna try to see whether my query works even though it shows only 1 data out... – user3766044 Jul 04 '14 at 08:38
  • @user3766044 if your query is correct and there is no output it could only mean that either pymongo could not match the query or the db is empty. try printing `{ userInputFirst : stringTest}` and run the query manually on mongodb terminal – Ashoka Lella Jul 04 '14 at 08:45
  • @AshokaLella I am sure my query is correct as there is results coming out if i were to type it on my mongoDB command console. It only happens when i tried to use pymongo, it does not show any results. My MongoConnection would show query: {text: "/you/i"} but there is no results returning back to me – user3766044 Jul 04 '14 at 08:52
  • @user3766044 try using `find_one()` instead of `find()`. It uses the same syntax as `find` and returns only the first result (where as find returns a cursor). – Ashoka Lella Jul 04 '14 at 08:54
  • I think this page will help : http://stackoverflow.com/questions/3483318/performing-regex-queries-with-pymongo. Using regex in pymongo is different than in mongoshell. – kranteg Jul 04 '14 at 09:00
  • @AshokaLella find_one() doesn't work for me... – user3766044 Jul 04 '14 at 09:02
  • @kranteg Thanks! Regex works for me now! – user3766044 Jul 04 '14 at 09:10

2 Answers2

0

To set regex with pymongo, you should use this :

query = db.fyp.find({ userInputFirst : {'$regex':userInputSecond, '$options' : 'i'}})

Check http://docs.mongodb.org/manual/reference/operator/query/regex/

kranteg
  • 931
  • 1
  • 7
  • 15
0

your find function is not good !when you define a find function you must pass the value to function and according to that retrieve other info's ! i give this example to you that i use it for myself !

my collection contains this dic for a word and its meaning :

  dic={"word":word,
      "mean":mean}

and my find function is this :

 def find_word(self,searched_word):
  #print self.dbh.users.find_one({"word":"example"})# for find a dictionary 
  users = self.dbh.users.find({"word":searched_word})
  for user in users:
   return user.get("mean")

now in your query if stringTest = "/" + userInputSecond + "/i" actually exist in your dictionary you can pass stringTest to your function and also i propose use str(stringTest) depending on your collection saving !

Mazdak
  • 105,000
  • 18
  • 159
  • 188