1. Python操作字符串数组插入元素。
# pip install pymongo==3.12.3
# 数组示例 strArr = ['a','b','c']
from pymongo import MongoClient
mongoClient = MongoClient(DB_IP,
username='mongo',
password='password',
authSource='admin',
authMechanism='SCRAM-SHA-1')
db = mongoClient["pacific"]
document = db["resObject"]
updateInfo = { "$pull": "strArr":"d"}}
result = document.update_one({'deviceId':deviceId}, updateInfo)
2. Python操作对象数组插入元素。
# pip install pymongo==3.12.3
# 数组示例 objArr = [{'K':'a','V':'1'},{'K':'b','V':'2'}]
from pymongo import MongoClient
mongoClient = MongoClient(DB_IP,
username='mongo',
password='password',
authSource='admin',
authMechanism='SCRAM-SHA-1')
db = mongoClient["pacific"]
document = db["resObject"]
updateInfo = { "$pull": {"objArr":{"K":"c","V" : 3}}}
result = document.update_one({'deviceId':deviceId}, updateInfo)
特别说明:本文内容参考自必应搜索(百度搜索)。
谢谢关注