查找嵌套文档并相应地更新它

Posted

技术标签:

【中文标题】查找嵌套文档并相应地更新它【英文标题】:Finding a nested document and updating it accordingly 【发布时间】:2020-12-26 20:39:40 【问题描述】:

稍微更新了方法,还是不行,但我认为我走在正确的道路上......

const condition =  'assets._id': assetId ;
    const updateData = 
      assetName: editAssetData.assetName,
      assetQuantity: editAssetData.assetQuantity,
      singleQuantityPrice: editAssetData.singleQuantityPrice,
      totalQuantityPrice: editAssetData.totalQuantityPrice,
    ;
    const options =  new: true ;

    const newAsset = await Catalog.findOneAndUpdate(condition,  $set: updateData , options).exec();

我正在尝试创建用于更新单个文档的路由。

这里是catalogModel,我想根据收到的assetId更新相应的资产...

const catalogSchema = new Schema(
  assetType:  type: String, required: true ,
  description:  type: String, required: false ,
  creator:  type: Schema.Types.ObjectID, required: true, ref: 'User' ,
  assets: [
    assetName:  type: String, required: false ,
    assetQuantity:  type: Number, required: false ,
    singleQuantityPrice:  type: Number, required: false ,
    totalQuantityPrice:  type: Number, required: false ,
  ],
);

这是我的 editAssetData 对象的样子:


  assetName: 'goldis',
  assetQuantity: '1',
  singleQuantityPrice: '1',
  totalQuantityPrice: '1'

这是我的查询,它不起作用。我尝试了其他方法,而不是更新单行,而是用 editassetData 替换了资产中的所有文档。

 const 
    assetId,
    editAssetData,
   = req.body;

    const newAsset = await Catalog.updateOne(
       'assets._id': assetId ,
      
        $set: 
          assetName: editAssetData.assetName,
          assetQuantity: editAssetData.assetQuantity,
          singleQuantityPrice: editAssetData.singleQuantityPrice,
          totalQuantityPrice: editAssetData.totalQuantityPrice,
        ,
      ,
    );

    const asset = await newAsset.save();

我得到的响应是: ok: 0, n: 0, nModified: 0 ... 表示没有找到和修改任何内容。

我尝试查看我的病情是否有任何发现,但没有。我不明白为什么不...

这是一个文档的样子,例如:

_id: 'the catalog object id'
assetType: "gold"
description: "test gold assets"
creator: 'the user object id'
assets: [ _id: 'the asset object id', assetName: "goldisnice", assetQuantity: 1, singleQuantityPrice: 1200, totalQuantityPrice: 1200  ]

【问题讨论】:

你的需求可以写成纯Mongo DB查询吗? @Mallik 是的,它可以 @Gabriel 你试过 'assets._id': mongoose.types.ObjectId(assetId) 吗? @mickl 是的,我也试过了 @Gabriel,我在答案部分添加了一个 mongo DB 查询。希望这对你有用。 【参考方案1】:
//MongoDB query run from mongo shell on windows10
//sample data for catalog Schema collection
> db.catalogSchema.find().pretty();

        "_id" : ObjectId("5f5791b0636fe5b2e945cd48"),
        "assetType" : "Mouse",
        "description" : "Logitech Mouse 120v with laser",
        "creator" : "johnd",
        "assets" : [
                
                        "assetName" : "logitech1",
                        "assetQuantity" : 12,
                        "singleQuantityPrice" : 100,
                        "totalQuantityPrice" : 1200
                ,
                
                        "assetName" : "microsoft1",
                        "assetQuantity" : 15,
                        "singleQuantityPrice" : 200,
                        "totalQuantityPrice" : 3000
                ,
                
                        "assetName" : "goldis",
                        "assetQuantity" : "11",
                        "singleQuantityPrice" : "1",
                        "totalQuantityPrice" : "1"
                
        ]

//update , need to use $ to access array elements
> db.catalogSchema.updateOne(      "assets.assetName":"goldis",      $set:"assets.$.assetQuantity":"22"      );
 "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 
> db.catalogSchema.find().pretty();

        "_id" : ObjectId("5f5791b0636fe5b2e945cd48"),
        "assetType" : "Mouse",
        "description" : "Logitech Mouse 120v with laser",
        "creator" : "johnd",
        "assets" : [
                
                        "assetName" : "logitech1",
                        "assetQuantity" : 12,
                        "singleQuantityPrice" : 100,
                        "totalQuantityPrice" : 1200
                ,
                
                        "assetName" : "microsoft1",
                        "assetQuantity" : 15,
                        "singleQuantityPrice" : 200,
                        "totalQuantityPrice" : 3000
                ,
                
                        "assetName" : "goldis",
                        "assetQuantity" : "22",
                        "singleQuantityPrice" : "1",
                        "totalQuantityPrice" : "1"
                
        ]

//update , need to use $ to access array elements(another collection field as example)
> db.catalogSchema.updateOne(      "assets.assetName":"goldis",      $set:"assets.$.assetName":"goldis New"      );
 "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 
> db.catalogSchema.find().pretty();

        "_id" : ObjectId("5f5791b0636fe5b2e945cd48"),
        "assetType" : "Mouse",
        "description" : "Logitech Mouse 120v with laser",
        "creator" : "johnd",
        "assets" : [
                
                        "assetName" : "logitech1",
                        "assetQuantity" : 12,
                        "singleQuantityPrice" : 100,
                        "totalQuantityPrice" : 1200
                ,
                
                        "assetName" : "microsoft1",
                        "assetQuantity" : 15,
                        "singleQuantityPrice" : 200,
                        "totalQuantityPrice" : 3000
                ,
                
                        "assetName" : "goldis New",
                        "assetQuantity" : "22",
                        "singleQuantityPrice" : "1",
                        "totalQuantityPrice" : "1"
                
        ]

>

【讨论】:

谢谢,我会试试你的方法,但我真的看不出你和我的区别。我稍微更新了我的方法,现在看起来像这样:...我已经更新了我的帖子

以上是关于查找嵌套文档并相应地更新它的主要内容,如果未能解决你的问题,请参考以下文章

查找并更新数组中列出的许多文档

MongoDB通过***属性和嵌套数组键查找文档,并返回匹配文档的一部分

PL/SQL 程序将一一检查库存以查找项目并相应更新

按子文档 ID 查找并更新其数据

Word已存文档中,将原有内容的字体,字号作相应改变,加标题,插入,替换某符号等word文档中怎么替换步骤

如何在 MongoDB 中查找文档、更新文档并插入新文档?