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?

8
  • Change your query to {'$regex': /^ja/} and try again. Commented Nov 3, 2018 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! Commented Nov 3, 2018 at 22:19
  • Thanks for the swift reply @RicoChen - This didn't work for me. Commented Nov 3, 2018 at 22:22
  • @RicoChen That's exactly the same thing. Examples are on the $regex documentation to show why there is no difference. Not the problem the question is asking about anyway. Commented Nov 3, 2018 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? Commented Nov 3, 2018 at 22:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.