我有一个看起来像这样的文档:
{
"_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'
},
},
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句