I'm using Pymongo to perform some upsert queries on a for loop, but since the queries are taking too much time to execute, i tried to do the same task using a bulk write operation. However, i'm not familiar with the syntax, and i'm having some problems:
According to the docs, i need to define an array of operations, so i did the following:
Queries = [[{'Unix': 1596873600.0}, {'$set': {'Unix': 1596873600.0, 'O': '11586.08000000', 'H': '11801.72000000', 'L': '11562.17000000', 'C': '11775.52000000', 'V': '9066.55659000', 'market': 'BTCUSDT'}, 'upsert': True}], [{'Unix': 1596888000.0}, {'$set': {'Unix': 1596888000.0, 'O': '11775.52000000', 'H': '11808.27000000', 'L': '11706.39000000', 'C': '11738.10000000', 'V': '6628.24686700', 'market': 'BTCUSDT'}, 'upsert': True}...]]
db['myCol'].bulk_write(Queries)
This gives me a is not a valid request
error. Can anyone help me out on the syntax of this query?I'm using PyMongo. Thanks in advance!
You need to use the bulk operators, in your case UpdateOne()
from pymongo import MongoClient, UpdateOne
db = MongoClient()['mydatabase']
Queries = [UpdateOne({'Unix': 1596873600.0}, {'$set': {'Unix': 1596873600.0, 'O': '11586.08000000'}}, upsert=True),
UpdateOne({'Unix': 1596888000.0}, {'$set': {'Unix': 1596888000.0, 'O': '11775.52000000'}}, upsert=True)]
db['myCol'].bulk_write(Queries)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.