从 mongo 的子数组中查找特定值

发布于 2025-01-20 15:05:13 字数 1621 浏览 3 评论 0原文

我只想从

{
        "_id" : "625305190f48a6512a18220d",
        "artist" : "AC/DC",
        "albums" : [
                {
                        "title" : "High Voltage",
                        "year" : 1976
                },
                {
                        "title" : "Back in Black",
                        "year" : 1980
                },
                {
                        "title" : "Dirty Deeds Done Dirt Cheap",
                        "year" : 1976
                },
                {
                        "title" : "Highway to Hell",
                        "year" : 1979
                }
        ],
        "singers" : "Bon Scott"
}

我首先尝试插入的

db.getCollection('first').insertMany([])

数据中获取 1976 年的值,然后使用以下语句过滤掉 1976 年以下代码

db.first.findOne({"albums.year":1976})

并得到此输出:

{
        "_id" : "625305190f48a6512a18220d",
        "artist" : "AC/DC",
        "albums" : [
                {
                        "title" : "High Voltage",
                        "year" : 1976
                },
                {
                        "title" : "Back in Black",
                        "year" : 1980
                },
                {
                        "title" : "Dirty Deeds Done Dirt Cheap",
                        "year" : 1976
                },
                {
                        "title" : "Highway to Hell",
                        "year" : 1979
                }
        ],
        "singers" : "Bon Scott"
}

为什么它给出的年份不是 1976 年 我只想过滤1976 年

i just want the values with year 1976 from the data

{
        "_id" : "625305190f48a6512a18220d",
        "artist" : "AC/DC",
        "albums" : [
                {
                        "title" : "High Voltage",
                        "year" : 1976
                },
                {
                        "title" : "Back in Black",
                        "year" : 1980
                },
                {
                        "title" : "Dirty Deeds Done Dirt Cheap",
                        "year" : 1976
                },
                {
                        "title" : "Highway to Hell",
                        "year" : 1979
                }
        ],
        "singers" : "Bon Scott"
}

what i tried first i insert using this statements

db.getCollection('first').insertMany([])

and then to filter out with year 1976 the following code

db.first.findOne({"albums.year":1976})

and got this output:

{
        "_id" : "625305190f48a6512a18220d",
        "artist" : "AC/DC",
        "albums" : [
                {
                        "title" : "High Voltage",
                        "year" : 1976
                },
                {
                        "title" : "Back in Black",
                        "year" : 1980
                },
                {
                        "title" : "Dirty Deeds Done Dirt Cheap",
                        "year" : 1976
                },
                {
                        "title" : "Highway to Hell",
                        "year" : 1979
                }
        ],
        "singers" : "Bon Scott"
}

why is it giving years which are not 1976 i want filter only the year which are 1976

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

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

发布评论

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

评论(1

逆流 2025-01-27 15:05:13

$ project之后查找

db.collection.find({
  "albums.year": 1976
},
{
  "albums.$": 1
})

mongoplay

$project after find

db.collection.find({
  "albums.year": 1976
},
{
  "albums.
quot;: 1
})

mongoplayground

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