How to fix an issue with MongoDB score placed first in query and causing error

255 views
Skip to first unread message

Yury

unread,
Sep 13, 2015, 8:03:11 PM9/13/15
to mongodb-user
Hi,

I have similar code as in PyMongo documentation on cursors here https://api.mongodb.org/python/current/api/pymongo/cursor.html

Here is the code from doc page.

cursor = db.test.find(
    {'$text': {'$search': 'some words'}},
    {'score': {'$meta': 'textScore'}})

When I run this code with Windows Python 2.7 PyMongo 3.0.3 and also 3.0.2 I get the following MongoDB error "OperationFailure: database error: Can't canonicalize query: BadValue unknown operator: $meta".
Looking at the cause I was able reproduce this error if I use MongoDB client and put "
{'score': {'$meta': 'textScore'}}" before "$text", like this.

db.test.find({ score: { $meta: "textScore" } }, { $text: { $search: "
some words" }})

Back in Python, if I print dictionary with query keys I see "score" key first and "$text" key second.
I guess this is causing PyMongo to form wrong query and subsequently an error.

Is there a work around for this issue?

Thanks

Asya Kamsky

unread,
Sep 14, 2015, 12:37:42 AM9/14/15
to mongodb-user
Use ordered dictionary aka SON class.




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/1918a8ad-628b-43ca-ac6f-d0be26d782e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luke Lovett

unread,
Sep 14, 2015, 4:35:35 PM9/14/15
to mongodb-user
I think the syntax of your text search might be incorrect. There should be two separate Python dictionaries as arguments to find(): the first is the query {'$text': {'$search': 'some words'}} and the second is the projection {'score': {'$meta': 'textScore'}}. Make sure that you are passing two separate arguments and not a single dictionary with both query/projection combined.
Reply all
Reply to author
Forward
0 new messages