我有一个Mongodb文档,我想用从表单收到的一些信息进行更新。 Mongodb中的原始文档具有以下数据:
.Cells(FndDt.Row, FndNum.Column) = "P"
从表格中获得的信息如下(其结构始终相同):
On Error Resume Next
为了从集合Option Explicit
Sub chrisellis250()
Dim Dt, Urn, i As Long, x As Long, lr As Long, lc As Long: x = 2
Dim colwidth As Long
Dim FndDt As Range, FndNum As Range, Dat As Date, Num As String, Loc As String
Dat = TimeValue("00:00:00")
Application.ScreenUpdating = False
With Sheet2
lr = .Cells(.Rows.Count, 2).End(xlUp).Row
.Range(.Cells(2, 1), .Cells(.Rows.Count, 1)).AdvancedFilter xlFilterCopy, , .Range("E1"), True
With .Range("E1").CurrentRegion: Dt = .Value: End With
Sheet1.Range("A3").Resize(UBound(Dt) - 1) = .Range("E2:E" & UBound(Dt)).Value: .Columns(5).Clear
Sheet1.Range("A3").Resize(UBound(Dt) - 1).Interior.ColorIndex = 15
.Range(.Cells(2, 2), .Cells(.Rows.Count, 2)).AdvancedFilter xlFilterCopy, , .Range("E1"), True
With .Range("E1").CurrentRegion: Urn = .Value: End With
For i = 1 To 2
Sheet1.Cells(2, x).Resize(, UBound(Urn) - 1) = Application.WorksheetFunction.Transpose(.Range("E2:E" & UBound(Urn)).Value)
If i = 1 Then colwidth = 8.3 Else colwidth = 55
Sheet1.Cells(2, x).Resize(, UBound(Urn) - 1).ColumnWidth = colwidth
If x = 2 Then Sheet1.Cells(1, x) = "URN" Else Sheet1.Cells(1, x) = "XXXXX"
Sheet1.Cells(1, x).Resize(, UBound(Urn) - 1).MergeCells = True
Sheet1.Cells(1, x).Resize(, UBound(Urn) - 1).Interior.ColorIndex = 15
x = x + UBound(Urn) - 1
Next i
.Columns(5).Clear
For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
If .Range("B" & i) <> "" Then
Dat = .Range("A" & i): Num = .Range("B" & i): Loc = .Range("C" & i)
With Sheet1
.Range("B3").Resize(lr, UBound(Urn) - 1).Font.Name = "Wingdings 2"
lc = .Cells(2, .Columns.Count).End(xlToLeft).Column
Set FndDt = .Range("A:A").Find(Dat, LookIn:=xlValues, lookat:=xlWhole)
Set FndNum = .Range(.Cells(2, 1), .Cells(2, lc)).Find(Num, LookIn:=xlValues, lookat:=xlWhole)
.Cells(FndDt.Row, FndNum.Column) = "P": .Cells(FndDt.Row, FndNum.Column).Font.Color = vbGreen
On Error Resume Next
If Not .Cells(FndDt.Row, FndNum.Column + UBound(Urn) - 1) Like "*" & Loc & "*" Then
.Cells(FndDt.Row, FndNum.Column + UBound(Urn) - 1) = IIf(.Cells(FndDt.Row, FndNum.Column + UBound(Urn) - 1) = "", Loc, .Cells(FndDt.Row, FndNum.Column + UBound(Urn) - 1) & "," & Loc)
End If
End With
End If
Next i
With Sheet1
With .Range("B3").Resize(UBound(Dt) - 1, UBound(Urn) - 1)
.SpecialCells(xlCellTypeBlanks).Font.Color = vbRed: .SpecialCells(xlCellTypeBlanks).Value = "O":
End With
With .Range("B3").Offset(, UBound(Urn) - 1).Resize(UBound(Urn) - 1, UBound(Urn) - 1)
.SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = 15
End With
AddOutsideBorders .Range("A1").Resize(UBound(Dt) + 1, 1 + ((UBound(Urn) - 1) * 2))
With .Cells
.Columns.AutoFit
.HorizontalAlignment = xlCenter
.RowHeight = 25
End With
End With
End With
Application.ScreenUpdating = True
End Sub
Public Function AddOutsideBorders(rng As Range)
With rng.Borders
.LineStyle = xlContinuous
.Color = vbBlack
.Weight = xlThin
End With
End Function
中更新此文档,我正在执行以下操作:
{"id":1, "name": "James", "surname1": "Adams"} [id is univoque]
这将为id = 1
name = "James"
surname1 = "Adams"
surname2 = ""
synced = 1
创建一个空字段。这是我的问题:在Mongodb中仅将那些不为空的字段插入的最pythonic方法是什么?可以使用内部Mongodb逻辑来完成此操作,还是只能通过对输入数据进行一些Python检查来完成?
答案 0 :(得分:1)
诀窍是在加载之前pop()记录中的项目。以此为例:
from pymongo import MongoClient
mongoClient = MongoClient('localhost:27017').db
# Setup some test data
for i in range(3):
mongoClient["foo"].insert({"id": i, "name": "James", "surname1": "Adams"})
# Simulate your incoming record
record = {"id": 1, "name": "James", "surname1": "Adams", "surname2": "", "synced": 1}
# Remove any empty items
for k, v in list(record.items()):
if v == '' or v is None:
record.pop(k)
mongoClient["foo"].update({"id": record['id']}, {"$set": record})
for item in mongoClient["foo"].find({}, {"_id": 0}):
print(item)
结果:
{'id': 0, 'name': 'James', 'surname1': 'Adams'}
{'id': 1, 'name': 'James', 'surname1': 'Adams', 'synced': 1}
{'id': 2, 'name': 'James', 'surname1': 'Adams'}
答案 1 :(得分:0)
批发文档更新(替换整个数据库文档)非常昂贵。如果您有办法检测应用程序中文档的更改,则可以为数据库提供目标更新。这个问题最初是如何更新数据库中的文档的,以便删除null或空字段以减小文档的整体大小。大概是为了提高性能并减少磁盘空间消耗。既令人钦佩的目标。但这就是问题–我们不仅要在磁盘上拥有结构良好的文档,还必须考虑如何管理这些数据。执行批量更新和替换整个文档无疑使客户端的开发更加容易,但是却给数据库引擎带来了负担。 MongoDB支持最大16MB的文档,因此文档越大,替换批发文档的影响越大。显然,有针对性的更新是可取的,但是我们该怎么做呢?我们如何知道传入文档与数据库中的字段之间有哪些不同?确实,这更多是一个哲学问题。我们可以查询数据库并与传入的文档进行比较,但这又对数据库产生了影响。但是请考虑一下-对于我们要更新的任何文档,我们必须首先从数据库中获取它-对吗?否则,它是一个新文档(插入而不是更新)。如果确实从数据库中获取信息,那么我们就有能力跟踪我们对文档所做的更改。如果是这样,那么我们传入的数据可能只是一组更改,而不是整个文档本身。同样,这是哲学,而不是科学。因此,请考虑更改输入数据而非实际文档的情况。我们可以构造指令以取消设置已删除字段的位置,并更新已修改但仍保留它们的目标字段。最初的问题是关于如何创建一个“ pythonic”解决方案来剥离空白字段,但是这个问题确实是更深入的跟踪变更问题的表层。