我想要 Mongo DB 脚本,它列出具有特定字段的所有集合,无论嵌套级别如何,这意味着如果我正在搜索字段 empId,它可以直接在一个集合中使用,并且在其他集合中可以嵌套
Example:
case 1:{
empid:1234,
empName:test
}
case 2(it can be nested in object1 like )
{
object1:{
emplId:1234,
empName :test
}
}
case 3(it can be nested one object-n like )
{
object-n:{
emplId:1234,
empName :test
}
}
我在下面写了一个脚本,如果该字段嵌套在另一个对象中,则该脚本不会搜索(如果字段未嵌套,它将搜索,这意味着仅适用于 case-1,它不适用于 case2 和 case3)
db.getCollectionNames().forEach(function(collection) {
if (db[collection].count({empId: {$exists: true}}) > 0) {
print(collection);
}
});
任何人都可以帮助查询以查找 mongodb 中具有特定字段的所有集合,无论其嵌套级别如何
const $query= {
$or: [
{
empid: {
$exists: true
}
},
{
"object1.empid": {
$exists: true
}]
for(let i = 0 ; i < the_bigger_n ; i++)
{
query.$or.push(
{
[`object-${i}.empid`]: {
$exists: true
}
)
}
db.collection.find($query)