简体   繁体   English

MONGODB 如果字段存在或什么都不做则聚合转换

[英]MONGODB Aggregation Convert if field exists or do nothing

I need convert a field in MONGODB but if the field/path doesn't exist, I don't want it to be created.我需要转换 MONGODB 中的一个字段,但如果该字段/路径不存在,我不想创建它。 I try using $$remove, $setfield, but without success in both.我尝试使用 $$remove、$setfield,但都没有成功。 Please, someone with knowledge on MongoDB, help me.请 MongoDB 上有知识的人帮助我。

Note: check the images please.注意:请检查图像。 I tried to use $$remove instead of "null" but still nothing我尝试使用 $$remove 而不是 "null" 但仍然没有

[{
        "$addFields": {
            "fieldExists": {
                "$cond": [{
                        "$ifNull": ["$documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot.vBC", null]
                    }, true, false]
            }
        }
    }, {
        "$addFields": {
            "convertResult": {
                "$cond": [{
                        "$eq": ["$fieldExists", true]
                    }, {
                        "$convert": {
                            "input": "$documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot.vBC",
                            "to": "decimal",
                            "onError": "$documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot.vBC",
                            "onNull": "$documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot.vBC"
                        }
                    }, null]
            }
        }
    }, {
        "$addFields": {
            "documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot.vBC": "$convertResult"
        }
    }
]

exists convert result replceRoot exists转换结果replceRoot

ExistsMongoPlaygroud NotExistsMongoPLayground存在MongoPlaygroud NotExistsMongoPLayground

Use $set to conditionally update the documentoAlterado object with the $convert result.使用$set有条件地使用$convert结果更新documentoAlterado object。

db.collection.aggregate([
  {
    "$addFields": {
      "documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot.vBC": {
        "$convert": {
          "input": "$documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot.vBC",
          "to": "decimal",
          "onError": "$$REMOVE",
          "onNull": "$$REMOVE"
        }
      }
    }
  },
  {
    "$set": {
      "documentoAlterado": {
        "$cond": {
          "if": {
            $eq: [
              {},
              "$documentoAlterado.nfeProc.NFe.infNFe.total.ICMSTot"
            ]
          },
          "then": {
            // keep the other fields you want
            "cteProc": "$documentoAlterado.cteProc"
          },
          "else": "$documentoAlterado"
        }
      }
    }
  }
])

Mongo Playground蒙戈游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mongodb 尝试执行 $lookup 和 $unwind 时聚合不返回任何内容 - Mongodb Aggregation returning nothing when trying to do $lookup and $unwind 当字段和值存在时,Mongodb find()查询不返回任何内容 - Mongodb find() query returns nothing while field and value exists 检查弹性搜索嵌套聚合下是否存在字段 - Checking if field exists under an elasticsearch nested aggregation MongoDB 聚合 - $group 一个字段按逻辑或 - MongoDB aggregation - $group one field by logical or 检查数据记录是否已经存在(如果没有=创建|如果是=什么都不做) - Check if data record already exists (if no = create | if yes = do nothing) 使用$ exists和$的MongoDB仍然返回没有字段的文档 - MongoDB using $exists and $and still return documents without field 如何检查mongodb集合中特定_id的字段是否存在? - How can I check if a field exists for a specific _id in mongodb collections? 检查mongoDb集合中是否存在特定字段,但不包括一条记录 - Check if particular field exists in mongoDb Collection,excluding one record 检查之前是否已经存在字段以在 MongoDB / NodeJS 上创建文档 - check if already exists field before to create a document on MongoDB / NodeJS MongoDB:创建聚合管道 - MongoDB: Create an aggregation pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM