将当前记录与Python中的MongoDB现有记录进行比较

时间:2017-07-15 06:47:01

标签: python mongodb pymongo

我正在使用python逐行遍历cvs文件。我需要将当前记录的2列与MongoDB中的现有记录进行比较。如果不存在,请将其插入mongo,否则;需要将当前记录的所有字段与mongoDB中的现有记录进行比较,当前记录将插入到旧记录的位置,并且只有字段中的更改将保存在同一文档中的历史记录json中。

MongoDB中的现有记录:

{ 
  "_id" : ObjectId("59661c4d5e2bb8a9c80e74b8"), 
  "ID" : 149, 
  "UID" : "2017-06-01__ccm-401__238AC3E",
  "Date" : "2017-06-01", 
  "Timestamp" : "2017-06-01 08:00:14",
  "UCM" : "ccm-401",
  "Description" : "SJC08-1-LOBBY", 
  "Site" : "SJC", 
  "Building" : "SJC08", 
  "Floor" : 1, 
  "Room_Name" : "LOBBY", 
  "MAC" : "SEP001DA238AC3E"
}

当前记录:

{
  "ID" : 149, 
  "UID" : "2017-06-05__ccm-401__238AC3E",
  "Date" : "2017-06-01", 
  "Timestamp" : "2017-06-01 08:00:14",
  "UCM" : "ccm-402",
  "Description" : "SJC08-1-LOBBY",
  "Site" : "SSC",
  "Building" : "SJC08",
  "Floor" : 1, 
  "Room_Name" :"LOBBY",
  "MAC" : "SEP001DA238AC3E"
}

此处验证字段为“描述”和“MAC”。如果当前记录的这两个字段与MongoDB中的现有记录相同,则需要比较记录的其他字段。在这种情况下,差异在于ID,UCM,站点字段,因此需要维护如下所示的更改字典...

COLL {
'uid':  
'mac':
'name':
'ip':
'status':
'date':
.
.
'config_history':
[
      {
       'date':
       'status':
       'ip':
       .
       .
       },
     {
      'date':
      'status':
      'ip':
      .
      .
     }
 ]
}

注意:MongoDB是远程访问服务器,因此无法像本地机器操作那样进行操作

1 个答案:

答案 0 :(得分:0)

我们假设您有一个CSV,其中的列由;分隔,您可以通过这种方式与mongo文档进行比较:

csv_lines = open('myfile.csv', 'r').read().split('\n')
# Iterate over lines
for line in csv_lines:
 # Break the line to get the columns
 cols = line.split(';')
 # Build an object for comparison
 csv_dict = {
  'UID' : cols[0],
  'MAC' : cols[1],
  # etc, etc. the key names are equal to the one in Mongo whenever it is possible
 }
 # compare
 cursor = collection.find({
  'UID' : csv_dict['UID'],
  # etc. with other match criterias
 })
 # Check if we have documents
 if cursor.count():
  # Yes, browse the results
  for document in cursor:
   # Perform deeper comparison by looking all the keys in csv_dict
   for key in csv_dict:
    # Is key missing in Mongo?
    if key not in document:
     # Missing key
    elif csv_dict[key] != document[key]
     # Different value