如果某些元素不存在或在mongoDB中进行更新,请按数组元素

发布于 2025-01-21 14:53:14 字数 906 浏览 0 评论 0原文

我必须根据我从req.value获得的字符串来更新文档中的数组。

My collection

[
  {
    "_id": 1,
    "key": [
      "1-value-1",
      "1-value-2"
    ]
  },
  {
    "_id": 2,
    "key": [
      "2-value-1",
      "2-value-2"
    ]
  }
]

I have another string newString. Let's assume two conditions:

  • when req.value is in the array in that case replace the array value with newString.
  • else rew.value不存在简单的推动newstring数组中的。

例如:我有一个newsTring = 1-Value-3req.value = 1-Value-2在这种情况下,替换阵列值1-Value-2到1-Value-3。 否则,如果req.value不在数组push newstring值中。

这是 Collection 我想在上执行操作。

感谢您提前回答。

I have to update an array inside a document on the basis of a string which i get from the req.value.

My collection

[
  {
    "_id": 1,
    "key": [
      "1-value-1",
      "1-value-2"
    ]
  },
  {
    "_id": 2,
    "key": [
      "2-value-1",
      "2-value-2"
    ]
  }
]

I have another string newString.
Let's assume two conditions:

  • when req.value is in the array in that case replace the array value with newString.
  • else rew.value is not present simple push the newString in the array.

Eg: I have a newString = 1-value-3 and req.value = 1-value-2 in that case replace array value 1-value-2 to 1-value-3.
Else if req.value isn't in array push newString value to array.

This is the collection on which i want to perform operations on.

Thanks for answering in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2025-01-28 14:53:14

使用管道更新

查询

  • 如果1-Value-2存在更改为1-value-3
  • else'push 1-value-3最后阵列(我认为您想要此)
  • 过滤器是否存在是否存在映射
  • 是否存在以更新以更新
  • concat以添加到阵列

playmongo (已更新,存在)
playmongo

update(
{"_id": {"$eq": 1}},
[{"$set": 
   {"v2-exists": 
     {"$ne": 
       [{"$filter": 
           {"input": "$key", "cond": {"$eq": ["$this", "1-value-2"]}}},
         []]}}},
 {"$set": 
   {"key": 
     {"$cond": 
       ["$v2-exists",
         {"$map": 
           {"input": "$key",
            "in": 
             {"$cond": 
               [{"$eq": ["$this", "1-value-2"]}, "1-value-3", "$this"]}}},
         {"$concatArrays": ["$key", ["1-value-3"]]}]}}},
 {"$unset": ["v2-exists"]}])

(检查是否存在),

find({"_id": {"$eq": 1}, "key": {"$elemMatch": {"$eq": "1-value-2"}}})

如果query1空结果发送此(按末尾按)

update(
{"_id": {"$eq": 1}},
{"$push": {"key": "1-value3"}})

else发送此(设置以替换旧值)

update(
{"_id": {"$eq": 1}},
{"$set": {"key.$[m]": "1-value-3"}},
{"arrayFilters": [{"m": {"$eq": "1-value-2"}}]})

Update with pipeline

Query

  • if 1-value-2 exists change to 1-value-3
  • else `push 1-value-3 in the end of the array (i think you want this)
  • filter to check if it exists
  • if exists map to update
  • else concat to add in the end of the array

Playmongo(update,exists)
Playmongo(push(concat), missing)

update(
{"_id": {"$eq": 1}},
[{"$set": 
   {"v2-exists": 
     {"$ne": 
       [{"$filter": 
           {"input": "$key", "cond": {"$eq": ["$this", "1-value-2"]}}},
         []]}}},
 {"$set": 
   {"key": 
     {"$cond": 
       ["$v2-exists",
         {"$map": 
           {"input": "$key",
            "in": 
             {"$cond": 
               [{"$eq": ["$this", "1-value-2"]}, "1-value-3", "$this"]}}},
         {"$concatArrays": ["$key", ["1-value-3"]]}]}}},
 {"$unset": ["v2-exists"]}])

Update operators

Query1(check if exists)

find({"_id": {"$eq": 1}, "key": {"$elemMatch": {"$eq": "1-value-2"}}})

If query1 empty result send this(push at the end)

update(
{"_id": {"$eq": 1}},
{"$push": {"key": "1-value3"}})

else send this (set to replace the old value)

update(
{"_id": {"$eq": 1}},
{"$set": {"key.$[m]": "1-value-3"}},
{"arrayFilters": [{"m": {"$eq": "1-value-2"}}]})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文