[MongoDB]: Querying multiple collections with a JavaScript

Imagine “my_newimage_db” database as multiple collections (say month as collection name in our example below) and what if I would like to search a value of a field and check whether which collection has a value for it.
For this scenerio, instead of querying all the collecitons – we can create a Java script function which will take care of it.
Feel free to suggect any modifictions/corrections for more accurate script – thanks.

 
> db
my_newimage_db
>
> show collections
apr
feb
fs.chunks
fs.files
jan
june
mar
may
system.indexes
>
> db.jan.find()
{ “_id” : ObjectId(“569645d04de7dd9f15d1b76e”), “x” : 1 }
>
> db.feb.find()
{ “_id” : ObjectId(“569645d64de7dd9f15d1b76f”), “x” : 1 }
>
>
> db.system.indexes.find()
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.fs.files” }
{ “v” : 1, “key” : { “filename” : 1 }, “name” : “filename_1”, “ns” : “my_newimage_db.fs.files” }
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.fs.chunks” }
{ “v” : 1, “unique” : true, “key” : { “files_id” : 1, “n” : 1 }, “name” : “files_id_1_n_1”, “ns” : “my_newimage_db.fs.chunks” }
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.jan” }
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.feb” }
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.mar” }
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.apr” }
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.may” }
{ “v” : 1, “key” : { “_id” : 1 }, “name” : “_id_”, “ns” : “my_newimage_db.june” }
>
> // now, I would like to search x value in all the existing collections.
>
>

Define a fuction as below with your requirement :
————————————————
function colListQuery() {
var tcol = new Array()
tcol= db.getCollectionNames();
for(var i = 1; i < tcol.length ; i++) {
query = “db.” + tcol[i] + “.find()”;

var docs= eval(query);
docs.forEach( function(doc, index){ print( “Database_Name:”, db, “Collection_Name:”, tcol[i], “x_value:”, doc.x, “_id:”, doc._id) });

}
}
Then call it with colListQuery() when you require it as shown in the illustaration.
> function colListQuery() {
… var tcol = new Array()
… tcol= db.getCollectionNames();
… for(var i = 1; i < tcol.length ; i++) {
… query = “db.” + tcol[i] + “.find()”;

… var docs= eval(query);
… docs.forEach( function(doc, index){ print( “Database_Name:”, db, “Collection_Name:”, tcol[i], “x_value:”, doc.x, “_id:”, doc._id) });

… }
… }
>
>
>
> colListQuery()

Database_Name: my_newimage_db Collection_Name: feb x_value: 1 _id: ObjectId(“569645d64de7dd9f15d1b76f”)
Database_Name: my_newimage_db Collection_Name: jan x_value: 1 _id: ObjectId(“569645d04de7dd9f15d1b76e”)
Database_Name: my_newimage_db Collection_Name: june x_value: 1 _id: ObjectId(“569645fd4de7dd9f15d1b773”)
Database_Name: my_newimage_db Collection_Name: mar x_value: 1 _id: ObjectId(“569645dd4de7dd9f15d1b770”)
Database_Name: my_newimage_db Collection_Name: may x_value: 1 _id: ObjectId(“569645f74de7dd9f15d1b772”)
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
Database_Name: my_newimage_db Collection_Name: system.indexes x_value: undefined _id: undefined
>
// note that if the collection doesn’t exist with the supplied field, you’ll see “undefined” as a value as above.

  • Ask Question