【发布时间】:2022-01-09 20:38:29
【问题描述】:
我正在尝试从嵌套的 JSON 对象中获取特定值。我尝试了以下方法,但在所有情况下,我都从所有嵌套对象中获取了所有特定值。 在这种情况下,我想从一位艺术家那里获得具体价格,id 为:1176704。
collection_name = dbname["Tattooparlor"]
cus_details = collection_name.aggregate([{"$match": {"artists._id": 1176704}}])
print(cus_details)
for r in cus_details:
print(r)
或
for r in collection_name.find({"_id": 9392991}, {"artists._id": 1176704}):
print(r)
for x in r["artists"]:
print(x["price"])
在所有情况下,它都会返回 1779、2730、4530 或完整的对象,而我只希望它返回 1779。
我的 JSON 对象如下所示
【问题讨论】:
-
试试$elemMatch 投影。
-
@prasad_ 我在尝试 $elemMatch "$elemMatch is not allowed in this atlas tier" 时收到此错误
-
这可能意味着您的 Atlas Cluster 层(例如 M0 等)具有一些有限的功能。您可以查找有关 限制 的 Atlas 文档。也许这可能有效:$ (projection).
标签: python json database mongodb