1

I have a collection where in a field contains an Array:

{
        "_id" : 1,
        "words" : [
                "9J",
                "4AAQSKZJRGABAAQAAAQABAAD",
                "MEDIAMESSAGE",
                "MMSMESSAGE",
                "NOTE",
                "HTTPS",
                "MEU_TEST_PIPE",
                "MEU_TEST_SEMICOLON",
                "EUSOUOELEMENTONOVO",
                "ELEMENTOMAISNOVO",
                "AAAH",
                "UAI",
                "BAH"
        ]
}

I need to get all array in "words" field. I would get like return an object look likes:

["9J", "4AAQSKZJRGABAAQAAAQABAAD", "MEDIAMESSAGE", "MMSMESSAGE", "NOTE", "HTTPS", "MEU_TEST_PIPE", "MEU_TEST_SEMICOLON", "EUSOUOELEMENTONOVO", "ELEMENTOMAISNOVO", "AAAH", "UAI", "BAH"]

How can I do that?

  • Use projection. – Ghazanfar Khan Mar 30 '20 at 15:57
  • I don't know a lot about mongodb. I wrote a query like: `db.mycollection.aggregate([{'$unwind': '$words'}])` where the return was: `{ "_id" : 1, "words" : "9J" } { "_id" : 1, "words" : "4AAQSKZJRGABAAQAAAQABAAD" } { "_id" : 1, "words" : "MEDIAMESSAGE" } { "_id" : 1, "words" : "MMSMESSAGE" } { "_id" : 1, "words" : "AAAH" } { "_id" : 1, "words" : "UAI" } { "_id" : 1, "words" : "BAH" }` –  Mar 30 '20 at 16:03
  • I answered this. – Ghazanfar Khan Mar 30 '20 at 16:05
  • 1
    @whoami, yes, is it! Thanks! –  Mar 30 '20 at 18:05

1 Answers1

0

Use this

db.mycollection.find({}).projection({"words": 1})
Ghazanfar Khan
  • 3,648
  • 8
  • 44
  • 89
  • Sorry about the last comment. In this case, the return is equal to db.mycollection.find({}). It returned all fields. –  Mar 30 '20 at 16:14
  • Then provide a filter inside find first like find({"_id" : 1}) . This will return only the document with id 1 – Ghazanfar Khan Mar 30 '20 at 16:47