总mongodb中用于筛选的Mongo查询

总mongodb中用于筛选的Mongo查询,mongodb,Mongodb,以上是我的mongodb模式,适合我的项目需求。现在我想要的是我必须搜索“标题”:“水果”只有水果的名字应该来自,从结果中我必须过滤公司“jessy transports” 我试过的是 { "_id":objectId(23651478), "name":"Tomatos", "company":"vikranth transports", "array":[ {"title":"Vegetables"} ],

以上是我的mongodb模式,适合我的项目需求。现在我想要的是我必须搜索“标题”:“水果”只有水果的名字应该来自,从结果中我必须过滤公司“jessy transports”

我试过的是

{
    "_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"
 }
这里我使用$text搜索,因为我试图在其中搜索“水果”。我试着过滤“杰西运输公司”。但我得到了所有的“杰西运输”而不是过滤的“水果”

确切地说,我想要的是,当我搜索“水果”时,我想要得到所有有“标题”的水果:数组中的“水果”

-->当我搜索“水果”时,我只希望公司名称有“jessy transports”,那么我必须得到所有有jessy transports的水果

-->当我搜索“水果”时,我只希望公司名称有“jessy transports”,从结果中我希望文本搜索“health”

尝试以下操作:

{"$text":{"$search":"jessy transports"}},{"array":{"$elemMatch":{"title":{"$in":[/Fruits/]}}}} 
使用并匹配查询。 它产生这样的结果

  db.YOUR_COLLECTION.aggregate(
    {$unwind:"$array"},
    {$match: {"array.title" :'fruits', 'company': 'jessy transports'}}).pretty()
试试这个:

{"$text":{"$search":"jessy transports"}},{"array":{"$elemMatch":{"title":{"$in":[/Fruits/]}}}} 
使用并匹配查询。 它产生这样的结果

  db.YOUR_COLLECTION.aggregate(
    {$unwind:"$array"},
    {$match: {"array.title" :'fruits', 'company': 'jessy transports'}}).pretty()