-6
{
    TypeList" : [ 
        {
            "TypeName" : "Carrier"
        },
        {
            "TypeName" : "Not a Channel Member"
        },
        {
            "TypeName" : "Service Provider"
        }
    ]
}

Question :

db.supplies.find("text", {search:"\"chann\" \"mem\""})

For above query I want display :

{
    TypeName" : "Not a Channel Member"
}

But I am unable to get my result.

What are changes I have to do in query . Please help me.

dnickless
  • 10,733
  • 1
  • 19
  • 34

2 Answers2

0

The below query will return your desired result.

db.supplies.aggregate([
   {$unwind:"$TypeList"},
   {$match:{"TypeList.TypeName":{$regex:/.*chann.*mem.*/,$options:"i"}}},
   {$project:{_id:0, TypeName:"$TypeList.TypeName"}}
])
0

If you can accept to get an output like this:

{
    "TypeList" : [ 
        {
            "TypeName" : "Not a Channel Member"
        }
    ]
}

then you can get around using the aggregation framework which generally helps performance by running the following query:

db.supplies.find(
{
    "TypeList.TypeName": /chann.*mem/i
},
{ // project the list in the following way
    "_id": 0, // do not include the "_id" field in the output
    "TypeList": { // only include the items from the TypeList array...
        $elemMatch: { //... where
            "TypeName": /chann.*mem/i // the "TypeName" field matches the regular expression
        }
   }
})

Also see this link: Retrieve only the queried element in an object array in MongoDB collection

dnickless
  • 10,733
  • 1
  • 19
  • 34
  • For list of objects is fine... If my collection contains multiple objects , lists and fields then what will be the query?.. pls help me. – Prabhakar Reddy Aug 22 '17 at 05:52
  • Please be more specific and give us some sample data, desired input and output. – dnickless Aug 22 '17 at 06:12
  • { "supplyId" : "26", "OrderId" : "46573", "status" : "Open", "statusId" : 201, "dateInitiated" : ISODate("2017-08-18T05:04:03.271Z"), "orderTitle" : "Order Title", "storeList" : [{ "storeId" : 402,"storeName" : "Banglore" }], "channelMemberTypeList" : [ { "channelMemberTypeId" : 502,"channelMemberTypeName" : "Distributor" }] "createDate" : ISODate("2017-08-18T05:05:50.647Z"), } – Prabhakar Reddy Aug 22 '17 at 06:56
  • { "supplyId" : "26", "OrderId" : "46573", "status" : "Open", "statusId" : 201, "date" : ISODate("2017-08-18T05:04:03.271Z"), "orderTitle" : "Order Title", "storeList" : [{ "storeId" : 402,"storeName" : "Banglore" }], "typeList" : [ { "typeId" : 502,"typeName" : "Distributor" }] }.. my collection like this... search value may be supplyid or status or date or storeName or typeName or may be combined also can enter ( 26 Distrib ---partial word)...then I want display documents which contains "26" and "Distributor" values . all fields are searchable fields. – Prabhakar Reddy Aug 22 '17 at 07:04
  • Please update your question to reflect all this and also be specific about what you want to get as an output. Give us some concrete examples - the better your examples are, the better our answers can be. – dnickless Aug 22 '17 at 18:59