MongoDB查询对象没有字段

发布于 2025-02-06 14:33:38 字数 455 浏览 1 评论 0原文

我正在尝试查询一个完整的mongoDB字段,但没有实习字段,例如此文档示例:

{
  _id: ObjectId('x8234728x8z8381'),
  Example: {
       field1: 'first',
       field2: 'second',
       field3: {
          field 3.1: 'third'
       }
  }
}

“在此处输入图像描述”

我想返回所有“示例”字段,但没有“ $ field2”。在MongoDB中有可能吗?

I'm trying to query a full mongodb field but without an intern field, like this document example:

{
  _id: ObjectId('x8234728x8z8381'),
  Example: {
       field1: 'first',
       field2: 'second',
       field3: {
          field 3.1: 'third'
       }
  }
}

enter image description here

I would like to return all "Example" field, but without "$field2". Is it possible in mongodb?

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

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

发布评论

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

评论(2

枕花眠 2025-02-13 14:33:38
db.collection.aggregate({
  "$project": {
    "Example.field2": 0
  }
})

您只是在投影中忽略它。

db.collection.aggregate({
  "$project": {
    "Example.field2": 0
  }
})

You just ignore it in projection.

送你一个梦 2025-02-13 14:33:38

您可以使用查找,然后不是要消除的字段“项目”。

db.collection.find({
  "_id": ObjectId("62a6144faf59381072fa83d6")
},
{
  "Example.field2": false
})

You can use find and then just not "project" the fields you want to eliminate.

db.collection.find({
  "_id": ObjectId("62a6144faf59381072fa83d6")
},
{
  "Example.field2": false
})

Try it on mongoplayground.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文