Reputation: 540
How can I create a dynamic view in MongoDB 3.2.21 to list all values of a field in all collections?
I have a field named "machine_name" that is present in most of my collections. I want to create a dynamic view that shows all values of this field in all collections, if exists.
So far, to list all the values that I am looking for, I came up with this code:
var machine_names = [];
db.getCollectionNames().forEach(function(collname) {
machine_names.push(db[collname].find({"machine_name":{$exists:true}}));
});
print(machine_names);
However, this results in a different format than I expected.
Upvotes: 1
Views: 59
Reputation: 1124
var machine_names = [];
db.getCollectionNames().forEach(function(collname) {
db.getCollection(collname).find({ machine_name: { $exists: true } } ,
{machine_name:1}).forEach(function(name) {
machine_names.push(name.machine_name)
});
});
print(machine_names);
Upvotes: 1