将查询从 sql 类型转换为 mongodb 类型

Convert query from sql type in mongodb type

我在 python 中使用 pymongo。 我在 psql 中有一个查询,如下所示:

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

现在我想将该查询转换为 mongodb 类型。 我做了这样的事情

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

这很好用,但我只想 select id13

我是这样想的:

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

但是没有work.Can有人帮助我吗?

您可以通过字典和要保留的字段传递第二个参数

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