简体   繁体   中英

MongoDB View of a Field in All Collections

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM