gpt4 book ai didi

node.js - 路径 "{ _id: 5bd587cffb6fc074abb12cb8,\n name:\' 处的值\',\n year:\' name 2\',\n id:\' 4\' }"102 "winner"转换为字符串失败

转载 作者:太空宇宙 更新时间:2023-11-04 03:18:37 25 4
gpt4 key购买 nike

我的数据库集合:

获奖者:

{
"_id": {
"$oid": "5bd58921fb6fc074abb12ce8"
},
"id": "301",
"winner": [
"101",
"102"
]
}

获胜者模型在这里

var winner = new Schema({
_id: String,
id: String,
winner:[ String ]
},
{
strict: false
});
var Winner = mongoose.model('winner', winner);

获胜者集合中获胜者数组中的值 101 和 102 应使用 {"sample":"data"} 进行更新

 var result = [{"id": "101" },{"id": "102" }];
var length = result.length;
for(var i=0;i<length;i++)
{
Winner.updateMany(
{ "winner" : result[i].id },
{ $set: { "winner" : result[i] } }, function (err,output) {
if(err)
{
console.log(err);
}
else
{
console.log(output);
}
}
);
}

预期输出:

获胜者集合应更新为

获奖者:

{
"_id": {
"$oid": "5bd58921fb6fc074abb12ce8"
},
"id": "301",
"winner": [
{"sample":"data"},
{"sample":"data"}
]
}

最佳答案

首先您要插入字符串数组,但后来您想要存储对象数组。 Mongoose 模式不允许这样做。您必须将架构从 winner:[ String ] 更新为 winner:[{sample: String}] 并首先存储对象数组。

{
"_id": {
"$oid": "5bd58921fb6fc074abb12ce8"
},
"id": "301",
"winner": [
{"sample": "101"},
{"sample": "102"}
]
}

编辑:您也想更新

Winner.updateMany(
{ "winner" : result[i].id },
{ $set: { "winner" : result[i] } },
.
.

Winner.updateMany(
{ "winner.sample" : result[i].id },
{ $set: { "winner" : result[i] } },
.
.

关于node.js - 路径 "{ _id: 5bd587cffb6fc074abb12cb8,\n name:\' 处的值\',\n year:\' name 2\',\n id:\' 4\' }"102 "winner"转换为字符串失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53035660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com