【问题标题】:Need to get the result of query combining the databases and its collections需要得到结合数据库及其集合的查询结果
【发布时间】:2021-03-04 05:30:00
【问题描述】:

我是 Mongodb 的新手,需要一些帮助(如下)

数据库:Test1

集合名称:info_123

{"_id" : ObjectId("5e975401c0178289eb6ac88a"),
    "name" : "hello_123",
    "model" : "omni",
    "status:true"
}

集合名称:info_abc

{"_id" : ObjectId("5e975401c0178289eb6ac08a"),
    "name" : "beta_a",
    "model" : "aqua",
    "status" :"true"
}

数据库:Test2

集合名称:info_A

{"_id" : ObjectId("5e975401ce178289eb6ac88a"),
    "name" : "hello_123",
    "model" : "aqua",
    "status" :"false"
}

集合名称:info_B

{"_id" : ObjectId("5e975401c0178289ebsac08a"),
    "name" : "alpha_123",
    "model" : "aqua",
    "status" :"false"
}

集合名称:Beta_B

{"_id" : ObjectId("5e975401c0178289ebsac08a"),
    "name" : "alpha_123",
    "model" : "aqua",
    "status" :"false"
}

问题:需要在集合名称类似于“info_”的所有数据库(test1、test2 等)中进行查询,并且该集合中的“name”字段不应包含“hello”然后分组通过“模型”字段,状态为“真”。

    db = db.getSiblingDB("admin");
    dbs = db.runCommand({ "listDatabases": 1 }).databases;
    
    // Iterate through each database and get its collections.
    dbs.forEach(function(database) {
        db = db.getSiblingDB(database.name);
        cols = db.getCollectionNames().filter(?);
    
        // Iterate through each collection.
        cols.forEach(function(col) {
            
        // Do something with each collection.
            print(col);
        });
    sudo
    });

【问题讨论】:

  • 你现在有什么问题?你以某种方式回答了你的问题。
  • @Ashkan:上述查询不完整。我无法按条件分组。
  • 您可以在 JS 中编写代码,然后从 shell 调用它。看看这个问题:stackoverflow.com/questions/39748101/…
  • @Ashkan : 请解决问题
  • 在上面的查询中基本上只有 1 条记录 "name" : "beta_a", "model" : "aqua", "status" :"true" } 应该显示

标签: mongodb


【解决方案1】:

您可以像这样获取数据库的名称:

const infoReg = /^info_/;
db.getMongo().getDBNames().forEach(dbName => {
    const siblingDb = db.getSiblingDB(dbName);
    const collectionList = siblingDb.getCollectionNames().filter(collectionName => {
        if (collectionName.match(infoReg)) {
            return true;
        }
    });
    // db.getCollection(collectionList[0]).find({});
    print(collectionList);
});

通过正则表达式,您可以找到符合您条件的集合。

【讨论】:

  • 我试过你的查询,我也试过如下查询 dbs.forEach(function(database) { db = db.getSiblingDB(database.name); cols = db.getCollectionNames().filter(function (col){return col.match(/info/i)}) // 遍历每个集合 cols.forEach(function(col) { // 对每个集合做一些事情 print(col); }); }) ;
  • 例如 db.getCollection('Info_992d0537-da97-48fc-bd3c-40e6e03055ab').aggregate([ {"$match"{{$not:{name:/hello/i},status :{$eq:"true"}}, {"$group" : {_id:{source:"$model"}, count:{$sum:1}}} ])
  • 我在与您在问题中解释的完全相同的数据库情况下运行了此查询。实在是忍不住多。 @dhairya
  • 感谢@ashkan,但我应该在哪里添加这个位以获得所需的结果。 : {"$match"{{$not:{name:/hello/i}, status:{$eq:"true"}}, {"$group" : {_id:{source:"$model"},计数:{$sum:1}}} ])
  • 我写的匹配指向正则表达式,而不是 MongoDb。把上面的几行一步一步写出来,用print得到答案并扩展它。也许它可以帮助你。 collectionList 包含具有您提到的标准的集合的名称。 @dhairya
猜你喜欢
  • 2021-08-12
  • 1970-01-01
  • 2019-02-05
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 1970-01-01
  • 2018-08-28
相关资源
最近更新 更多