整个上午我一直在努力。我正在尝试从数组中提取单个嵌套对象。我一直following along with the mongo query docs,但我无法返回所需的物体。
videocollection
{
"_id": ObjectID("95738ax1795b45f0hgn8dbfd8"),
"playlist": [
{
"videos": [
{
"title": "Video Title 1",
"slug": "video-1"
},
{
"title": "Video Title 2",
"slug": "video-2"
}
],
"related": [
{
....
}
]
}
]
}
当我在MongoDB CLI中运行以下查询时,我返回零结果或整个文档。
db.videocollection.find({}, { $elemMatch: {'playlist.videos.slug':'video-2'} } )
db.videocollection.find({}, {_id: 0, 'playlist.0.videos': {$elemMatch: {'slug': 'video-2' }}})
我正在尝试查询视频数组中的slug
。
答案 0 :(得分:0)
正确的查询是:
db.videocollection.find({ 'playlist.$.videos': { $elemMatch: { 'slug': 'video-2'} } })
$
是positional operator,代表数组中的任何索引。