由于源是数组,如何使用Icontains在ListField中搜索?

2024-05-14 07:37:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Django在MongoDB中查询数据。我的数据库中有此文档:

{
"source": "www.google.com",
"hierarchies": [],
"mainTags": [
    "Sobre a página",
    "Teste Tag",
    "Cartão de Crédito"
],
"email": "a@a.com",
"_id": {
    "$oid": "5ba7ffee2ef3f4139de74f3d"
},
"updated_at": {
    "$date": 1537725886501
},
"created_at": {
    "$date": 1537725886501
},
"title": "Relação de seguros entre 2017 e 2016",
"tableId": "relacao_de_seguros_entre_2017_e_2016_7836c6e2",
"relevance": 1.0,
"description": "este dataset tem como função mostrar a Relação de seguros entre 2017 e 2016",
"tags": []

}

我的条目应该是这样的:["Sobre", "Teste"]

我正在尝试查找我的Maintag包含我的条目值的所有文档。我就是这么做的:

    for word in keywordsArray:
        q |= Q(mainTags__icontains = word)
    objects = Metadata.objects.all().filter(q).order_by('relevance')

我也试过:

    objects = Metadata.objects.all().filter(Q(mainTags__in=mongoKeywords)).order_by('relevance')

但是当我把["Sobre", "Teste"]作为条目传递时,它什么也没找到。如何实现?你知道吗

谢谢!你知道吗


Tags: 文档comdateobjects条目deatword
1条回答
网友
1楼 · 发布于 2024-05-14 07:37:26

我不知道django,但这在正常的mongoshell查询中有效。它将找到标签为Sobre或Teste的所有文档。/我表示忽略案例搜索

db.collectionName.find({
"mainTags": {"$regex":/^Sobre$|^Teste$/i}
})

其他选择是:

db.collectionName.find({
    "mainTags": {"$in":["Sobre","Teste"]}
})

相关问题 更多 >

    热门问题