Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Arrays MongoDB更新/更新插入错误:";该路径必须存在于文档中才能应用数组更新;_Arrays_Mongodb_Mongoose_Upsert - Fatal编程技术网

Arrays MongoDB更新/更新插入错误:";该路径必须存在于文档中才能应用数组更新;

Arrays MongoDB更新/更新插入错误:";该路径必须存在于文档中才能应用数组更新;,arrays,mongodb,mongoose,upsert,Arrays,Mongodb,Mongoose,Upsert,我试图根据文档元素和文档中某些数组的元素更新/升级Mongo集合,但遇到了问题。我已经阅读了关于(看起来像:$[])和(看起来像:$[element])的MongoDB文档,我还阅读了关于Stackoverflow的类似问题,但我仍然无法理解 来自MongoDB的示例 MongoDB文档的工作示例是: 我想弄明白的是,如果myArray不是: [0,1] 是一个对象数组,如[{“num”:1,“name”:“X”},{“num”:2,“name”:“Y”}] 我的收藏示例: var myColl

我试图根据文档元素和文档中某些数组的元素更新/升级Mongo集合,但遇到了问题。我已经阅读了关于(看起来像:
$[]
)和(看起来像:
$[element]
)的MongoDB文档,我还阅读了关于Stackoverflow的类似问题,但我仍然无法理解

来自MongoDB的示例

MongoDB文档的工作示例是:

我想弄明白的是,如果myArray不是:
[0,1]
是一个对象数组,如
[{“num”:1,“name”:“X”},{“num”:2,“name”:“Y”}]

我的收藏示例:

var myCollection = 
[
  { "name": "Design", "price": 499,  "users": [{"username": "A", "profiles": 0} ,{"username": "B", "profiles": 2}, {"username": "C", "profiles": 9}  ], "face_to_face": 2 },
  { "name": "Launch", "price": 999,  "users": [{"username": "A", "profiles": 0} ,{"username": "R", "profiles": 6}, {"username": "X", "profiles": 10}  ], "face_to_face": 4 },
  { "name": "Scale",  "price": 1499, "users": [{"username": "B", "profiles": 0} ,{"username": "F", "profiles": 5}, {"username": "G", "profiles": 12}  ], "face_to_face": 8 }
]
我想用英语做什么:

const updateObj = {
"$set": {
"name":areaName
, "price": areaPrice
, "face_to_face": 0
,"users.$[element].username" : thisUserName 
,"users.$[element].profiles" : countProfiles
}

myCollection.updateMany({
       "name": areaName
       ,"users": {"$elemMatch": {"username": thisUserName}}
    }, updateObj, {
       arrayFilters: { "element.username": thisUserName  }],
       upsert: true
    }).exec();
查找与“name”匹配的文档,在这些文档中,如果有一个“users”元素具有“username”的给定值,则使用“profiles”的给定值更新该元素,但如果没有一个元素具有“username”的给定值,则将一个新元素(具有“username”和“profiles”的给定值)推送到数组中“用户”。如果“名称”上没有匹配的文档,则创建该文档(包括名称、价格和数组用户,其开头应有一个条目,其中包含用户名和配置文件的给定值

下面是我试图用来实现更新/升级的代码:

const updateObj = {
"$set": {
"name":areaName
, "price": areaPrice
, "face_to_face": 0
,"users.$[element].username" : thisUserName 
,"users.$[element].profiles" : countProfiles
}

myCollection.updateMany({
       "name": areaName
       ,"users": {"$elemMatch": {"username": thisUserName}}
    }, updateObj, {
       arrayFilters: { "element.username": thisUserName  }],
       upsert: true
    }).exec();
但这不起作用,并产生以下错误:

The path 'users' must exist in the document in order to apply array updates.
我怀疑这是因为我的查询没有指定“users”是数组

我也试过:

myCollection.updateMany({
       "name": areaName
       ,"users": [{"username": thisUserName}]
    }, updateObj, {
       arrayFilters: [ { "element.username": thisUserName  }],
       upsert: true
    }).exec();
myCollection.updateMany({
   "name": areaName
   ,"users": []
}, updateObj, {
   arrayFilters: [ { "element.username": thisUserName  }],
   upsert: true
}).exec();
但问题是(我认为)它不能正确地查询文档,因为“users”是一个包含多个键(username、profiles)的数组,而不是查询中给出的一个键(username)

我也试过:

myCollection.updateMany({
       "name": areaName
       ,"users": [{"username": thisUserName}]
    }, updateObj, {
       arrayFilters: [ { "element.username": thisUserName  }],
       upsert: true
    }).exec();
myCollection.updateMany({
   "name": areaName
   ,"users": []
}, updateObj, {
   arrayFilters: [ { "element.username": thisUserName  }],
   upsert: true
}).exec();
但是这部分的“upsert”不起作用。它正在创建一个文档,但是数组用户只是空的
[]
,而不是
[{“username”:thisername,“profiles”:countProfiles]。

帮忙


PS抱歉,我无法粘贴mongo DB沙盒或其他东西,找不到允许.update()的沙盒。

尝试将
updateMany
更改为
update

尝试将
updateMany
更改为
update

可能尝试
用户。$[element.username]:thisUserName
或尝试
用户。$[element]:{username:thisername,profiles:countProfiles}
第一个选项产生错误“更新中未使用用户的数组筛选器”"第二个错误与之前相同。在做了一些研究之后,mongodb似乎只支持根文档的upsert。您可以更新数组值,但没有服务器端功能将值upsert到数组中。是的,最终我将其转化为一系列步骤来完成,而不是一个upsert。May请尝试
“users.$[element.username]”:thisername
或尝试
“users.$[element]:{username:thisername,profiles:countProfiles}
第一个选项产生错误“更新中未使用用户数组筛选器”第二个错误与之前相同。在做了一些研究之后,mongodb似乎只支持根文档的upsert。您可以更新数组值,但没有服务器端功能将值upsert插入数组。是的,最终我将其转化为一系列步骤来完成相同的操作,而不是一个upsert。This不提供问题的答案。一旦你有足够的答案,你将能够;取而代之,.-这不提供问题的答案。一旦你有足够的答案,你将能够;取而代之-