【发布时间】: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