{
"_id":objectId(23651478),
"name":"Tomatos",
"company":"vikranth transports",
"array":[
{"title":"Vegetables"}
],
"description":"Vegitables are good to health"
},
{
"_id":objectId(45761244),
"name":"Apples",
"company":"jessy transports",
"array":[
{"title":"Fruits"}
]
"description":"Fruits are good to health, vegitables are also good to health"
},
{
"_id":objectId(45761244),
"name":"Nepal apples",
"company":"sai transports",
"array":[
{"title":"Vegetables-home-made"}
]
"description":"Fruits are good to health, vegitables are also good to health"
}
以上是我的项目要求的mongodb模态。现在我想要的是我必须搜索“标题”:“水果”只有水果名称应该来,结果我必须过滤公司“jessy transports”
我尝试的是
{"$text":{"$search":"jessy transports"}},{"array":{"$elemMatch":{"title":{"$in":[/Fruits/]}}}}
这里我使用了$ text搜索,因为我正在尝试搜索“Fruits”。我试图过滤“jessy transports”。但我得到了所有“jessy transports”,然后没有过滤“Fruits”
正是我想要的是当我搜索“水果”时我希望得到所有具有“标题”的水果:“水果”在阵列中。
- >当我搜索“水果”时,我只想要公司名称有“jessy transports”,那么我必须得到有jessy运输的水果。
- >当我搜索“水果”时,我只想要公司名称有“jessy transports”,结果我想要一个文本搜索“健康”
答案 0 :(得分:2)
试试这个:
db.YOUR_COLLECTION.aggregate(
{$unwind:"$array"},
{$match: {"array.title" :'fruits', 'company': 'jessy transports'}}).pretty()
使用$unwind然后匹配查询。 它产生这样的结果
{
"_id" : ObjectId("58076939309f9df8e6b59573"),
"name" : "Apples",
"company" : "jessy transports",
"array" : {
"title" : "Fruits"
},
"description" : "Fruits are good to health, vegitables are also good to health"
}