从 mongo 的子数组中查找特定值
我只想从
{
"_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$ project
之后查找
mongoplay
$project
afterfind
mongoplayground