当前位置:网站首页>Pymongo gets a list of data

Pymongo gets a list of data

2022-07-06 06:47:00 Cute Dai Ma

At present, there is no way to directly get the whole column of data , Therefore use for Loop data , For example, get all _id

[i['_id'] for i in coll.find({
    }, {
    "_id": 1})]

Sample code

from pymongo import MongoClient
import numpy as np

#  Specify database 
mongo_conn = MongoClient(host='localhost', port=27017)
mongo_db = mongo_conn.get_database("test")  #  Specify database 
coll = mongo_db.get_collection("people")

#  Add data 
[coll.insert_one({
    "num": i, "letter": np.random.choice(a=['a', 'b', 'c', 'd'])}) for i in range(5)]

#  Get a column of data :_id
all_num = [i['num'] for i in coll.find({
    }, {
    "num": 1})]  #  Get a list of data 
print(all_num)
原网站

版权声明
本文为[Cute Dai Ma]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060635060022.html