mongodb - mongo searching more than 1 collection for same criteria -


i have mongo db several collections contain json document formats shown below:

{    "questions": [      {        "questionentry": {          "id": 1,          "info": {            "seasonnumber": 1,            "episodenumber": 1,            "episodename": "days gone bye"          },          "questionitem": {            "thequestion": "q1",            "attachedelement": {              "type": 1,              "value": ""            }          },          "options": [            {              "type": 1,              "value": "o1"            },            {              "type": 1,              "value": "o1"            }          ],          "answer": {            "questionid": 1,            "answer": 1          },          "metatags": [            "season 1",            "episode 1",            "rick grimmes"          ]        }      },      {        "questionentry": {          "id": 1,          "info": {            "seasonnumber": 1,            "episodenumber": 1,            "episodename": "days gone bye"          },          "questionitem": {            "thequestion": "q2",            "attachedelement": {              "type": 1,              "value": ""            }          },          "options": [            {              "type": 1,              "value": "o2"            },            {              "type": 1,              "value": "o2"            }          ],          "answer": {            "questionid": 1,            "answer": 1          },          "metatags": [            "season 1",            "episode 1",            "rick grimmes",            "glenn rhee"          ]        }      }    ]  }

i'm able search questions.questionentry.questionitem.thequestion matching criteria with:

db.questions.find({"questions.questionentry.questionitem.thequestion" : "q1"},{'questions.$':1}).pretty()

this works questions collection how same search across multiple collections?

many thanks

to use same query across multiple collections may have use javascript bracket notation access collections in loop. example, following queries records database collections (using db.getcollectionnames() command) specified query:

use records var colls = db.getcollectionnames(), // collections in records db     query = {"questions.questionentry.questionitem.thequestion" : "q1"},     projection = {"questions.$": 1}; colls.foreach(function (collection){         var docs = db[collection].find(query, projection).toarray(); // use bracket notation     docs.foreach(function (doc){ printjson(doc); }); }) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -