0

How to search all the collections of a database-name: test for a word having length say 6

The sample data in collections is like:

{
    "_id": {
        "$oid": "5e0983863bcf0dab51f2872b"
    },
    "word": "never",
    "wordset_id": "a42b50e85e",
    "meanings": [{
        "id": "1f1bca9d9f",
        "def": "not ever",
        "speech_part": "adverb",
        "synonyms": ["ne'er"]
    }, {
        "id": "d35f973ed0",
        "def": "not at all",
        "speech_part": "adverb"
    }]
}

How can I query for all words with length of 6 across all collections of test?

I have tried this way but it is giving only first collection results:

@app.......
def fn():
    collections=db.collection_names()
    for collection in collections:
        data = db[collection].aggregate([
            {
                "$match": {
                    "$expr": {"$eq": [{"$strLenCP": "$word"}, n]}
                }
            }
        ])
    return render_template('lettersearch.html',data=data)

when I am printing data I get all cursors as:

<pymongo.command_cursor.CommandCursor object at 0x00000258F4F5B470>
<pymongo.command_cursor.CommandCursor object at 0x00000258F4F76BA8>
<pymongo.command_cursor.CommandCursor object at 0x00000258F4F6E5F8>
<pymongo.command_cursor.CommandCursor object at 0x00000258F4F8A6A0>
<pymongo.command_cursor.CommandCursor object at 0x00000258F4F8E048>

How to iterate over these objects and render in template as data?

KcH
  • 3,302
  • 3
  • 21
  • 46

2 Answers2

1

For each collection use $strLenCP with this aggregation:

db.collection.aggregate([
    {
        $match: {
            $expr: {$eq: [{$strLenCP: "$word"}, 6]}
        }
    }
])
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
  • Does it search all collections of db or should i have to mention collection name? – KcH Dec 31 '19 at 09:09
  • 1
    no, theres no such thing as searching all of the collections - you will have to change it to something like `db.getCollection("collection_name").aggregate` – Tom Slabbaert Dec 31 '19 at 09:12
  • mate , how to query such that get all results having `speech_part`="adverb" for eg – KcH Dec 31 '19 at 12:45
1

How to search all the collections of a database

You can achieve this in two steps:

  1. Retrieve all collections in the database - db.getCollectionNames()
  2. For each collection run the query below

Query:

db.collection.aggregate([
    {
        $match: {
            $expr: {
                $eq: [
                    {
                        $strLenCP: "$word"
                    },
                    6
                ]
            }
        }
    }
]);
user9408899
  • 4,202
  • 3
  • 19
  • 32
  • 2) point , after getting all collections how can I run query for each collection? – KcH Dec 31 '19 at 09:26
  • 1
    You can use this `db["yourCollectionName"].aggregate()`. @Codenewbie – user9408899 Dec 31 '19 at 09:27
  • Thanks a ton matet, only thing i want to overwrite in your answer is `db.list_collection_names()` to retrieve all collections – KcH Dec 31 '19 at 09:30
  • 1
    I think it is Python version, mine is `mongo shell` version. @Codenewbie – user9408899 Dec 31 '19 at 09:32
  • Any guiding link to mongo query examples? – KcH Dec 31 '19 at 10:03
  • Check out mongodb docs, it has example for every command. @Codenewbie – user9408899 Dec 31 '19 at 10:06
  • one more question, how do you iterate over all collections the above query ? when I am trying i just get the result for first collection – KcH Dec 31 '19 at 10:13
  • Store your collections in a list (i.e. collections). Then, define a for loop `for collection in collections: db[collection].aggregate([])` – user9408899 Dec 31 '19 at 10:17
  • updated in my question , did i do something wrong there? – KcH Dec 31 '19 at 10:19
  • when I try to print the `data` above i am getting all `cursor objects` , updated in my question , how can I loop over them and pass all `data` in to `dataall` variable to render in template? – KcH Dec 31 '19 at 10:37
  • Is there a way to do that mate, i have been stuck here – KcH Dec 31 '19 at 11:33
  • Check out this: https://stackoverflow.com/questions/28968660/how-to-convert-a-pymongo-cursor-cursor-into-a-dict, or consider asking a new question. @Codenewbie – user9408899 Dec 31 '19 at 11:37