MongoDB Shell命令从嵌套数组中删除元素

时间:2018-12-01 14:01:03

标签: mongodb

我正在尝试根据其值删除MongoDB嵌套数组中的元素...

下面是将数据加载到的插入查询

from math import floor, fabs
from PIL import Image, ImageSequence

def transform_image(original_img, crop_w, crop_h):
  """
  Resizes and crops the image to the specified crop_w and crop_h if necessary.
  Works with multi frame gif and webp images also.

  args:
  original_img is the image instance created by pillow ( Image.open(filepath) )
  crop_w is the width in pixels for the image that will be resized and cropped
  crop_h is the height in pixels for the image that will be resized and cropped

  returns:
  Instance of an Image or list of frames which they are instances of an Image individually
  """
  img_w, img_h = (original_img.size[0], original_img.size[1])
  n_frames = getattr(original_img, 'n_frames', 1)

  def transform_frame(frame):
    """
    Resizes and crops the individual frame in the image.
    """
    # resize the image to the specified height if crop_w is null in the recipe
    if crop_w is None:
      if crop_h == img_h:
        return frame
      new_w = floor(img_w * crop_h / img_h)
      new_h = crop_h
      return frame.resize((new_w, new_h))

    # return the original image if crop size is equal to img size
    if crop_w == img_w and crop_h == img_h:
      return frame

    # first resize to get most visible area of the image and then crop
    w_diff = fabs(crop_w - img_w)
    h_diff = fabs(crop_h - img_h)
    enlarge_image = True if crop_w > img_w or crop_h > img_h else False
    shrink_image = True if crop_w < img_w or crop_h < img_h else False

    if enlarge_image is True:
      new_w = floor(crop_h * img_w / img_h) if h_diff > w_diff else crop_w
      new_h = floor(crop_w * img_h / img_w) if h_diff < w_diff else crop_h

    if shrink_image is True:
      new_w = crop_w if h_diff > w_diff else floor(crop_h * img_w / img_h)
      new_h = crop_h if h_diff < w_diff else floor(crop_w * img_h / img_w)

    left = (new_w - crop_w) // 2
    right = left + crop_w
    top = (new_h - crop_h) // 2
    bottom = top + crop_h

    return frame.resize((new_w, new_h)).crop((left, top, right, bottom))

  # single frame image
  if n_frames == 1:
    return transform_frame(original_img)
  # in the case of a multiframe image
  else:
    frames = []
    for frame in ImageSequence.Iterator(original_img):
      frames.append( transform_frame(frame) )
    return frames

现在,当它具有“ field”:“ Fix Version”时,我想删除“ items”数组中的元素。因此在Mongo --shell中尝试了以下查询。

db.records.insert({"key" : "ABC-111",
                "listChgLogs" : [ { 
                    "timestamp" : NumberLong("1543217249133"), 
                    "user" : "kopi", 
                    "changelog" : { 
                        "id" : "744683", 
                        "items" : [ { 
                            "field" : "Fix Version", 
                            "fieldtype" : "jira", 
                            "fieldId" : "fixVersions", 
                            "from" : "23555", 
                            "to" : null, 
                            "fromSting" : "Release 4.0.15", 
                            "toSting" : null 
                        }, 
                        { 
                            "field" : "Fix Version", 
                            "fieldtype" : "jira", 
                            "fieldId" : "fixVersions", 
                            "from" : null, 
                            "to" : "23539", 
                            "fromSting" : null, 
                            "toSting" : "Release 4.0.14" 
                        } ] 
                    } 
                }, 
                { 
                    "timestamp" : NumberLong("1542830553816"), 
                    "user" : "kaneshen", 
                    "changelog" : { 
                        "id" : "741833", 
                        "items" : [ { 
                            "field" : "status", 
                            "fieldtype" : "jira", 
                            "fieldId" : "status", 
                            "from" : "13516", 
                            "to" : "6", 
                            "fromSting" : "Log Work", 
                            "toSting" : "Closed" 
                        } ] 
                    } 
                }, 
                { 
                    "timestamp" : NumberLong("1542807125525"), 
                    "user" : "kaneshen", 
                    "changelog" : { 
                        "id" : "741526", 
                        "items" : [ {      "field" : "status", 
                            "fieldtype" : "jira", 
                            "fieldId" : "status", 
                            "from" : "10507", 
                            "to" : "6", 
                            "fromSting" : "In Review", 
                            "toSting" : "Closed" 
                        } ] 
                    } 
                } 
            ] 
            })

但是我得到了错误:

db.records.update({"key" : "ABC-111"},{$pull: {"listChgLogs.changelog.items.fields": "Fix Version"}})

db.records.update({"key" : "ABC-111"},{$pull: {"listChgLogs.changelog.items": {"fields": "Fix Version"}}})

类似地...

WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 28,
"errmsg" : "Cannot use the part (changelog) of (listChgLogs.changelog.items.fields) to traverse the element ({listChgLogs: [ { timestamp: 1543217249133, user: \"kopi\", changelog: { id: \"744683\", items: [ { field: \"Fix Version\", fieldtype: \"jira\", fieldId: \"fixVersions\", from: \"23555\", to: null, fromSting: \"Release 4.0.15\", toSting: null }, { field: \"Fix Version\", fieldtype: \"jira\", fieldId: \"fixVersions\", from: null, to: \"23539\", fromSting: null, toSting: \"Release 4.0.14\" } ] } }, { timestamp: 1542830553816, user: \"kaneshen\", changelog: { id: \"741833\", items: [ { field: \"status\", fieldtype: \"jira\", fieldId: \"status\", from: \"13516\", to: \"6\", fromSting: \"Log Work\", toSting: \"Closed\" } ] } }, { timestamp: 1542807125525, user: \"kaneshen\", changelog: { id: \"741526\", items: [ { field: \"status\", fieldtype: \"jira\", fieldId: \"status\", from: \"10507\", to: \"6\", fromSting: \"In Review\", toSting: \"Closed\" } ] } } ]})"
}
})

不确定我缺少什么...请帮助...

0 个答案:

没有答案