将现有的外部文档移至内部文档

发布于 2025-01-30 07:53:39 字数 332 浏览 3 评论 0原文

给定与以下文档相似的文档集合,

{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
}

该文档名称是一个唯一字段

,我如何更新所有现有文档并添加其他字段,从而看起来像这样

{
"userDetails":{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
},
"userLikes":{
"likes": "food",
"plays": "ball"
},

}

Given a collection of documents similar to the following document

{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
}

where name is a unique field

How do I update all existing documents and add additional fields so it looks like this

{
"userDetails":{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
},
"userLikes":{
"likes": "food",
"plays": "ball"
},

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2025-02-06 07:53:39

您需要运行setunset在所有文档上使用updatemany

游乐场

db.collection.update({},
{
  "$set": {
    "userDetails.description": "$description",
    "userDetails.name": "$name",
    "userLikes.like": "abs"
  },
  "$unset": {
    description: 1,
    name: 1
  }
})

You need to run set and unset on all docs with updateMany

Playground

db.collection.update({},
{
  "$set": {
    "userDetails.description": "$description",
    "userDetails.name": "$name",
    "userLikes.like": "abs"
  },
  "$unset": {
    description: 1,
    name: 1
  }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文