0

I am a Beginner in MongoDB and I hope my following question is clear to all.

Consider the following document-

{
'_id': ObjectId("A"),
components:[
    0: {
        index: 21,
        abc:22,
        person: [
            {id:23, first:'Jack',last:'Sparrow', location:'USA,New York'},
            {id:24, first:'Jack',last:'Ryan', location:'USA,New Jersey'},
            {id:25, first:'Jill',last:'Ryan', location:'USA,New Jersey'},
            {...}
        ]
    },
    1: {
        index: 22,
        abc:23,
        person: [
            {id:12, first:'Andy',last:'Sparrow', location:'USA,New York'},
            {id:14, first:'Kate',last:'Ryan', location:'USA,New Jersey'},
            {...}
        ]
    },
]
}

Now I am trying to write a MongoDB query to perform regex operations on the person array in my document so that I get the following answer

{
    person:[
        {id:23, first:'Jack',last:'Sparrow', location:'USA,New York'},
        {id:24, first:'Jack',last:'Ryan', location:'USA,New Jersey'}
    ]
}

OR

{
components:[
    person:[
        {id:23, first:'Jack',last:'Sparrow', location:'USA,New York'},
        {id:24, first:'Jack',last:'Ryan', location:'USA,New Jersey'}
    ]
]
}

I thought that since its a deeply nested query its better to use aggregate.

1st Try:

db.collection.aggregate(
    {'$unwind': '$components'},
    {'$match' : 
         '_id': ObjectId("A"), 
         'components.index':'21',
         'components.first':{'$regex':'^ja'}
    }
)

But this doesn't seem to work. I want to use regex strictly for my case to work. I tried to do the same thing with the location field.

2nd Try: I also tried doing the same thing as above and adding to the aggregate Pipeline another unwind for Person Array like following.

db.collection.aggregate(
    {'$unwind': '$components'},
    {'$match' : 
         '_id': ObjectId("A"), 
         'components.index':'21',
         'components.first':{'$regex':'^ja'}
    },
    {'$unwind': '$components.person'},
)

But this didn't change anything.

My Questions:

1) Is it possible to do this?

2) Is there a solution for this using find query?

3) If I want to unwind person array, How to do it?(Unwind deeply nested array)

4) How do I use Regex in this situation?

Anurag P
  • 337
  • 3
  • 11
  • Change your query to `{'$regex': /^ja/}` and try again. – Rico Chen Nov 03 '18 at 22:17
  • First part is the query really should be using `$elemMatch`, but that is slightly negated in the specific since you use `_id` so it's only one "document" anyway. Second part is "queries" do not "filter arrays". For that you need the aggregation framework, and for a "regex" you have to do `$unwind` since there is no "aggregation expression" for a regex. Yet! – Neil Lunn Nov 03 '18 at 22:19
  • Thanks for the swift reply @RicoChen - This didn't work for me. – Anurag P Nov 03 '18 at 22:22
  • @RicoChen That's exactly the same thing. Examples are on the [`$regex`](https://docs.mongodb.com/manual/reference/operator/query/regex/) documentation to show why there is no difference. Not the problem the question is asking about anyway. – Neil Lunn Nov 03 '18 at 22:23
  • @NeilLunn Thank You for replying. I am trying to unwind person array. but `$components.person` didn't work for me. I guess this should do the trick right? So according to me in aggregation I should do 1- unwind component, 2- match document and object in component, 3- unwind person, 4- match object in person using $regex. Is this correct? – Anurag P Nov 03 '18 at 22:24
  • `{ "$unwind": "$components" },{ "$unwind": "$components.person" }` - you have an "array inside an array" and you cannot do that on **one step**. Each array needs `$unwind` separately. Make sure you read the [`$elemMatch`](https://docs.mongodb.com/manual/reference/operator/query/elemMatch/) related answer as well to understand what is wrong with your initial query. – Neil Lunn Nov 03 '18 at 22:26
  • To be clear, you need a "second" `$match` **after** both `$unwind` statements in order to "filter" the generated documents. But that should be mentioned in at least one answer of the "retrieve the queried elements" answers. – Neil Lunn Nov 03 '18 at 22:43
  • same condition is also not working in my case. is there any other way to get ragex applied on person array to search by its "first" key ??????. Please help me in this. – anshuman burmman Nov 08 '19 at 08:51

0 Answers0