0

I have a mongoDB collection, called myCollection, and inside myCollection there is a structure like this:

{
    "id": 1234,
    "posts": [
       [
          {
             "id": "0.0",
             "name": "john",
             "message": "hello"
          },
          {
             "id": "0.1",
             "name": "jane",
             "message": "good morning"
          },
          {
             "id": "0.2",
             "name": "josh",
             "message": "good evening"
          }
       ],
       [
          {
             "id": "1.0",
             "name": "mark",
             "message": "good lunch"
          }
       ],
       [
          {
             "id": "2.0",
             "name": "john",
             "message": "bye bye"
          },
          {
             "id": "2.1",
             "name": "mark",
             "message": "hi"
          }
       ]
    ]
}

Can anyone tell me how I can query this structure to obtain the ENTIRE array that contains a specific object? For example, launching a query around this specific object:

{
    "id": "2.0",
    "name": "john",
    "message": "bye bye"
}

I would to obtain this entire array:

       [
          {
             "id": "2.0",
             "name": "john",
             "message": "bye bye"
          },
          {
             "id": "2.1",
             "name": "mark",
             "message": "hi"
          }
       ]

1 Answer 1

1

Hope below query is help :

db.myCollection.aggregate([
  { $match : { "id": 1234}},
  { $unwind  : '$posts'},
  {$match : {
        'posts' : { $elemMatch : { 'id' : '2.0',name : 'john'}} 
     }
  },
  { $unwind : '$posts'},
  { $project : {
      'id' : '$posts.id',
      name : '$posts.name',
      message :'$posts.message',
      _id : 0
     }
  }
])
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for your reply! myCollection also contains other documents besides the one with "id":"1234". So, how can I specify inside the query that you have suggested me that I want to search the array that contains the { 'id' : '2.0',name : 'john'} object only inside the document with "id":"1234"?
Answer Updated! check if you are passing "1234" or 1234.
thank you so much for your answer. Unfortunately it doesn't work .. :( The query does not return the array [ { "id": "2.0", "name": "john", "message": "bye bye" }, { "id": "2.1", "name": "mark", "message": "hi" } ] ..... :(
In particular, with the query that you have suggested me, I retrive the whole structure inside the collection. Can you tell me how I can get only the entire array [ { "id": "2.0", "name": "john", "message": "bye bye" }, { "id": "2.1", "name": "mark", "message": "hi" } ] ?
Thank you very much @the_mahasagar for you help. Your solution works correctly and is exactly what I needed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.