以下是一个丧心病狂的mongoDB shell query转换成pymongo的查询语句:涉及and condition和date查询。
###mongoShell
db.shenwan_industry.find({ $and:[{"start_date": {"$lte": new Date("2017-04-06T00:00:00.000Z")}},{"cancel_date": {"$gt": new Date("2017-04-06T00:00:00.000Z")}},{"version":2 }]})
import pymongo
from pymongo import MongoClient
import pandas as pd
from datetime import datetime
startd = datetime(2017,4,6,0,0,0)
endd = datetime(2017,4,6,0,0,0)
if __name__ == '__main__':
client = MongoClient('mongodb://root:root@110.30.05.07:27017/')
db = client.test
collection_new = db.shenwan_industry
df_new = pd.DataFrame(collection_new.find_one(),index=[0]) #index=[0] column title的生成。
for col in collection_new.find({ "$and":[{"start_date": {"$lte": startd}},{"cancel_date": {"$gt": endd}},{"version":2 }]}):
dp = pd.DataFrame(col,index=[0]) #index=[0] column title的生成。把col dictionary变dataFrame
df_new = df_new.append(dp,ignore_index=False)
df_new.to_csv('AllinMongo.csv')