Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDb-添加密钥以更新查询本身中的对象_Mongodb - Fatal编程技术网

MongoDb-添加密钥以更新查询本身中的对象

MongoDb-添加密钥以更新查询本身中的对象,mongodb,Mongodb,我有一个priceGroup收藏 { "_id" : ObjectId("5e67dc3bbf237bb991f677be"), "productType" : { "_id" : ObjectId("5e67cddc5150bf069b000ab0"), "name" : "ERW Pipes", "code" : "03" }, "grade" : { "_id" : ObjectId("5e

我有一个priceGroup收藏

  {
    "_id" : ObjectId("5e67dc3bbf237bb991f677be"),
    "productType" : {
        "_id" : ObjectId("5e67cddc5150bf069b000ab0"),
        "name" : "ERW Pipes",
        "code" : "03"
    },
    "grade" : {
        "_id" : ObjectId("5e67cddc5150bf069b000abd"),
        "name" : "MS",
        "code" : "01"
    },
    "make" : {
        "_id" : ObjectId("5e67861d2df8eac1fa614e8b"),
        "name" : "Primary",
        "code" : "01"
    },
    "attributeGroupType" : "classification",
    "attributeGroupValue" : "LIGHT",
    "prices" : [ 
        {
            "_id" : ObjectId("5e67e4401a484e0d3a89f316"),
            "region" : {
                "_id" : ObjectId("5e67b1f972419a86996b26fe"),
                "name" : "Bhilai",
                "code" : "BH"
            },
            "price": 1200
            "isAutocalculated" : false
        }, 
        {
            "_id" : ObjectId("5e67e4401a484e0d3a89f317"),
            "region" : {
                "_id" : ObjectId("5e67b1f972419a86996b26ff"),
                "name" : "Pune",
                "code" : "PU"
            },
            "lastPrice" : 1230,
            "price" : 1245
            "isAutocalculated" : true
        }
    ],
    "createdAt" : ISODate("2020-03-10T18:28:11.831Z"),
    "updatedAt" : ISODate("2020-03-10T19:02:24.150Z"),
    "__v" : 0
} 
我想更新所有地区给定单据的价格。 我正在像这样更新价格

 db.priceGroups.updateOne(
            {
                'productType.name': 'ERW Pipes',
                'grade.name': 'MS',
                'make.name': 'Primary',
                attributeGroupValue: 'LIGHT'
            },
            {
                prices: [
    {
        region: {
            _id: '5e67b1f972419a86996b26fe',
            name: 'Bhilai',
            code: 'BH',
            __v: 0
        },
        price: '1230',
        isAutocalculated: false
    },
    {
        region: {
            _id: '5e67b1f972419a86996b26fe',
            name: 'Pune',
            code: 'PU',
            __v: 0
        },
        price: '1260',
        isAutocalculated: false
    }
]
            }
        );
我想用prices数组的每个元素添加/更新字段lastPrice。lastPrice是项目更新前的价格。我知道我可以先执行findOne并向update对象添加lastPrice,然后调用updateOne。但是,有没有办法只使用一个查询就可以做到这一点呢?

更新后,将显示所需的结果

  {
    "_id" : ObjectId("5e67dc3bbf237bb991f677be"),
    "productType" : {
        "_id" : ObjectId("5e67cddc5150bf069b000ab0"),
        "name" : "ERW Pipes",
        "code" : "03"
    },
    "grade" : {
        "_id" : ObjectId("5e67cddc5150bf069b000abd"),
        "name" : "MS",
        "code" : "01"
    },
    "make" : {
        "_id" : ObjectId("5e67861d2df8eac1fa614e8b"),
        "name" : "Primary",
        "code" : "01"
    },
    "attributeGroupType" : "classification",
    "attributeGroupValue" : "LIGHT",
    "prices" : [ 
        {
            "_id" : ObjectId("5e67e4401a484e0d3a89f316"),
            "region" : {
                "_id" : ObjectId("5e67b1f972419a86996b26fe"),
                "name" : "Bhilai",
                "code" : "BH"
            },
            "price" : 1230,
            "lastPrice" : 1200,
            "isAutocalculated" : false
        }, 
        {
            "_id" : ObjectId("5e67e4401a484e0d3a89f317"),
            "region" : {
                "_id" : ObjectId("5e67b1f972419a86996b26ff"),
                "name" : "Pune",
                "code" : "PU"
            },
            "price" : 1260,
            "lastPrice" : 1245,
            "isAutocalculated" : true
        }
    ],
    "createdAt" : ISODate("2020-03-10T18:28:11.831Z"),
    "updatedAt" : ISODate("2020-03-10T19:02:24.150Z"),
    "__v" : 0
} 
期望输出解释 price中的第一个条目没有lastPrice,因此更新后的price值设置为新附加的“lastPrice”键,并且price使用新的传入价格进行更新。
第二个条目已经有一个lastPrice,因此更新后的price值被设置为lastPrice,并且price被更新为新的price。

为了获得您提供的文档,您只需运行

db.collection.updateMany(
   {},
   { $unset: { "prices.$[].lastPrice": "" } }
)

然而,这与你的问题完全不符。请提供正确的结果文档或重新表述您的问题。

您可以使用以下步骤:

db.collection.updateOne(
   { "prices._id": { $in: [ObjectId("5e67e4401a484e0d3a89f316"), ObjectId("5e67e4401a484e0d3a89f317")] } },
   [
      {
         $set: {
            prices: {
               $map: {
                  input: "$prices",
                  in: { $mergeObjects: ["$$this", { lastPrice: "$$this.price" }] }
               }
            }
         }
      }
   ]
)

db.collection.updateOne(
   {},
   { $set: { "prices.$[item].price": 1230 } },
   { arrayFilters: [{ "item._id": ObjectId("5e67e4401a484e0d3a89f316") }] }
)

db.collection.updateOne(
   {},
   { $set: { "prices.$[item].price": 1260 } },
   { arrayFilters: [{ "item._id": ObjectId("5e67e4401a484e0d3a89f317") }] }
)
如果您喜欢单一聚合,可以使用以下聚合:

db.collection.aggregate([
   {
      $set: {
         prices: {
            $map: {
               input: "$prices",
               in: { $mergeObjects: ["$$this", { lastPrice: "$$this.price" }] }
            }
         }
      }
   },
   {
      $set: {
         prices: {
            $map: {
               input: "$prices",
               in: {
                  $switch: {
                     branches: [
                        { case: { $eq: ["$$this._id", ObjectId("5e67e4401a484e0d3a89f316")] }, then: { $mergeObjects: ["$$this", { price: 1230 }] } },
                        { case: { $eq: ["$$this._id", ObjectId("5e67e4401a484e0d3a89f317")] }, then: { $mergeObjects: ["$$this", { price: 1260 }] } },
                     ],
                     default: "$$this"
                  }
               }
            }
         }
      }
   }
])

您的MongoDB版本是什么?可能您需要?如何定义“更新前的项目权限”?我在
prices
数组中没有看到任何日期/时间信息,也没有任何其他定义“lastPrice”的指标。请在更新后提供一个示例文档。@whoami我的MONGODB版本是4.0.10。@WernfriedDomscheit我已经用所需的更新结果更新了问题。对不起!我更正了所需输出以匹配问题,并解释了所需输出。请看你现在是否能理解