Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
Node.js 更新作为mongodb和nodejs中对象属性的数组的一项_Node.js_Mongodb - Fatal编程技术网

Node.js 更新作为mongodb和nodejs中对象属性的数组的一项

Node.js 更新作为mongodb和nodejs中对象属性的数组的一项,node.js,mongodb,Node.js,Mongodb,您好,我正在尝试用nodejs更新mongodb中的单个数组项。我在mongodb中的数据结构如下: { "_id": "5e818db59cedd232d83c66cb", "project": "projectX", "version": 1, "salesChannel": "channel42", "country": "Austria", "book": true, "categories": [ {

您好,我正在尝试用nodejs更新mongodb中的单个数组项。我在mongodb中的数据结构如下:

{
    "_id": "5e818db59cedd232d83c66cb",
    "project": "projectX",
    "version": 1,
    "salesChannel": "channel42",
    "country": "Austria",
    "book": true,
    "categories": [
      {
        "pieceItems": [
          {
            "category": "pieceItems",
            "identifier": "1.1",
            "registrationHints": "Scan the barcode OR register the itemNumber manually",
            "variant": "Tax-Group 19%",
            "additionalHints": "optionally provided"
          }
        ],
      },
      {
        "weightItems": [
          {
            "category": "weightItems",
            "identifier": "2.1",
            "registrationHints": "Scan the barcode OR register the itemNumber manually",
            "variant": "Tax-Group 19%",
            "additionalHints": "optionally provided"
          },
          {
            "category": "weightItems",
            "identifier": "2.2",
            "registrationHints": "Scan the barcode OR register the itemNumber manually",
            "variant": "Tax-Group 19%",
            "additionalHints": "optionally provided"
          }
        ],
      }
    ]
}
这是我尝试过的代码,但数据库中没有任何更改:

exports.editCategoryInBook = function (body, project, salesChannelName, country) {
  mongoClient.connect(mongoUrl, async (err, client) => {
    assert.equal(null, err)
    const db = client.db(dbName)
    var categoryName = Object.keys(body)[0]
    var updateCategory = `categories.$.${categoryName}`
    var categoryValue = body[categoryName]
    console.log(`$set: { ${updateCategory}: categoryValue }`)
    db.collection(project).updateOne({
      "book": true,
      "salesChannel": salesChannelName,
      "country": country
    }, {
      $set: { [updateCategory]: categoryValue },
      $currentDate: { lastModified: true }
    })
    client.close()
  })
  return new Promise(function (resolve, reject) {
    resolve();
  });
}
这是传递给函数的主体变量:

{
    "pieceItems": [
    {
        "category": "Piece Item",
        "identifier": 1.11,
        "registrationHints": "Scan the barcode OR register the itemNumber manually; some more hints could follow",
        "variant": "Tax-Group 19%",
        "additionalHints": "some string hint to give bonus information about this item",
        "name": "Buntsteinputz 8508",
        "EAN": 4003498690070,
        "PLU": 3822584,
        "price": "51,95€"
    },
    {
        "category": "Piece Item",
        "identifier": 1.2,
        "registrationHints": "Scan the barcode OR register the itemNumber manually; some more hints could follow",
        "variant": "Tax-Group 7%",
        "additionalHints": "some string hint to give bonus information about this item",
        "name": "Buntsteinputz 8508",
        "EAN": 4003498690070,
        "PLU": 3822584,
        "price": "51,95€"
    }
  ]
}
我从console.log获得以下输出:

$set: { categories.$.pieceItems: categoryValue }
我得到以下错误:

MongoError: The positional operator did not find the match needed from the query.
at Function.create (C:\Users\rkrause\node_modules\mongodb\lib\core\error.js:43:12)
at toError (C:\Users\rkrause\node_modules\mongodb\lib\utils.js:149:22)
at C:\Users\rkrause\node_modules\mongodb\lib\operations\common_functions.js:376:39
at C:\Users\rkrause\node_modules\mongodb\lib\core\connection\pool.js:404:18
at processTicksAndRejections (internal/process/task_queues.js:82:9)

所以现在的问题是,它不想更新其中的所有内容,除非它匹配过滤器部分中的某个搜索查询。但我认为我们可以像这样欺骗它:

const WhatToSet=`categories.$.${categoryName}`
db.collection(project.updateOne)({
“书”:没错,
“salesChannel”:salesChannelName,
“国家”:国家,
[`categories.${categoryName}`]:{$exists:true}
}, {
$set:{[WhatToSet]:categoryValue},
$currentDate:{lastModified:true}
})

我希望这能有所帮助

为什么要使用
“param”:true
?@Ramesh ohhh谢谢!我忘了将其更改为
“book”:true
,并更新了另一个文件。。这就是我的错误很高兴我能帮上忙。每个人都会犯这些愚蠢的错误,只有其他人才能发现它们,因为它们一行一行地出现,而我们在检查自己的代码时不会这样做:)。是否要添加其他字段(EAN、PLU、price)到
pieceItems
数组中的现有子文档。@prasad\uuu是的,如果正文有差异,我想更新整个pieceItems数组。我已经更新了我的问题,你能回顾一下我的更改吗?也许它需要像“categories.$.pieceItems”一样,但我认为这可能是一个cyntax错误。我更新了答案。我想我们可以骗mongo做这件事:完美,非常感谢!我会接受你的回答。请将
`categories.${categoryName}`
WhatToSet
放在方括号中,因为没有方括号它就无法工作。我不确定我是否正确编辑了它,也不明白为什么会这样。你能详细说明一下吗?