0

I am working in python with pymongo. I have a query in psql that looks like this :

cursor.execute("select id13 from nc_durhamallwithorder where date2 >='2010-01-01' and date2<='2011-01-01'"),

Now i want to convert that query in mongodb type. I did something like this

cursor=mycol1.find({"$and": [ { "date2": { "$gte": "2010-01-01" } }, { "date2": { "$lte":"2011-01-01" } } ]  } )

This works fine but i want to select only id13

I thought something like this:

cursor=mycol1.find({"id13":1},{"$and": [ { "date2": { "$gte": "2010-01-01" } }, { "date2": { "$lte":"2011-01-01" } } ]  } )

But it doesnt work.Can someone help me?

1 Answer 1

1

You can use pass a second argument with a dict and the fields you want to keep

mycol1.find({"$and": [ 
        { "date2": { "$gte": "2010-01-01" } }, 
        { "date2": { "$lte":"2011-01-01" } } 
    ]},
    {"id13":1} 
)
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your reply.I am executing query with cursor so i did cursor=mycol1.find({"$and": [ { "date2": { "$gte": "2010-01-01" } }, { "date2": { "$lte":"2011-01-01" } } ]} ).fields(id13=1) and the output says :AttributeError: 'Cursor' object has no attribute 'fields'
I edited my question.I only added cursor function
Oh my bad, it's pymongo. I updatef my answer for your package
Now it works great.Thank you!If you have time i want to ask you one more thing.My date2 field is a string type.Why $gte and $lte work fine without date2 being double?
You're welcome. Since the dates are stored year-month-day... In this format, the alphabetical order will be the same as the chronological order.
|

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.