我有一个MongoDB集合,其中每个文档都具有以下格式:
{
"course_id": "course_id",
"language": "language",
"topics": [
{
"topic_id": "topic_id",
"topic_title": "topic_title",
"topic_description": "topic_description",
},
],
}
我要做的是在给定course_id
和language
的情况下检索一个数组(and only the array, not a document with the 102 field),其中每个元素只有topic_id
和topic_title
字段,例如:
[
{"topic_id": "id_1", "topic_title": "title1"},
{"topic_id": "id_2", "topic_title": "title2"},
]
为了检索only the array,我使用.distinct()
方法,如下所示:
result = db.topics_collection.distinct("topics", {"course_id": course_id, "language": language})
现在,我还需要过滤掉topic_description
字段,但我try 的以下查询不起作用:
result = db.topics_collection.distinct("topics", {{"course_id": course_id, "language": language}, {"topic_description": 0}})
有没有其他方法(可能也使用不同于.distinct()
的方法)也过滤掉topic_description
字段?