[英]How to update a specific field in an array that matches a query in a Mongodb document?
我有一個看起來像這樣的文檔:
{
"_id" : ObjectId("5f55fdede873f132ec09207e"),
"edition_id" : NumberInt(35889464),
"title" : "TEAM ROCKET EDITION",
"date" : "2020-09-19T10:00:00.000+0000",
"homescreen" : {
"image" : "https://media3.giphy.com/media/oOfLwhLyRUoRW/giphy.gif",
"song" : "5T8l7ciAo4H3NGFld8c9Ow"
},
"teams" : [
{
"team_id" : "8675309",
"team_name" : "",
"team_code" : ""
},
{
"team_id" : "7779311",
"team_name" : "",
"team_code" : ""
},
{
"team_id" : "79369254",
"team_name" : "",
"team_code" : ""
}
]
}
我正在嘗試形成一個 MongoDB 查詢,該查詢將找到匹配的team_id
並更新該數組項的team_name
。 我在這個結構中嘗試了幾件事:
import { connectToDatabase } from 'utils/mongodb';
export default async (req, res) => {
const { db } = await connectToDatabase();
const {
query: { edition_id, team_name, team_id },
} = req;
console.log({ req });
const edition = await db.collection('editions').findOneAndUpdate(
{ edition_id: parseInt(edition_id) },
{
$set: {
},
},
);
res.json(edition);
};
但我似乎無法制定正確的查詢。
請檢查此查詢,
const edition = await db.collection('editions').updateOne(
{ 'teams.team_id': '8675309' },
{
$set: {
'teams.$.team_name': 'team_name_value'
},
},
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.