pymongo 学习

  1. 查看一条记录,返回一条 dict 记录
    db.Account.find_one({"UserName":"keyword"})

  2. 查看某一列的一条记录(此时的1,也可以为True,表示只显示UserName这一列,为False时表示显示除了UserName这一列的其他所有列)
    db.Account.find_one({"UserName":"keyword"}, {"UserName":1})

  3. 查看多条记录,返回一个对像
    db.Account.find({"UserName":"keyword"})
    此时查看记录需:
    cursor = db.Account.find({"UserName":"keyword"})
    for data in cursor:
    print(data)

  4. 查看记录统计
    db.Account.find().count()

  5. 查询结果排序
    db.Account.find().sort("UserName") --默认为升序
    db.Account.find().sort("UserName", -1) --降序
    db.Account.find().sort("UserName", 1) --升序
    db.Account.find().sort([("UserName",pymongo.ASCENDING),("Email",pymongo.DESCENDING)]) --多列排序

  6. 添加记录
    db.Account.insert({"AccountID":21,"UserName":"libing"})

  7. 修改记录
    db.Account.update({"UserName":"libing"},{"$set":{"Email":"[email protected]","Password":"123"}})

  8. 删除记录
    db.Account.remove() -- 全部删除
    db.Test.remove({"UserName":"keyword"}) --删除UserName为keyword的所有记录

data_returns = portfolio_collection.find_one({'run-id': run_id},
sort=[('p-1-seq', pymongo.DESCENDING)])

mongo 参考: https://www.cnblogs.com/zhwl/p/3421084.html
pymongo 参考: https://www.cnblogs.com/libingql/archive/2011/06/15/2081545.html https://www.cnblogs.com/inns/p/7246168.html

猜你喜欢

转载自www.cnblogs.com/ronky/p/9670519.html