How to loop through collection from MongoDB Realm?

How to loop through collection from MongoDB Realm?
I want to loop through each collection to get data from them.
Something like getCollectionNames() in MongoShell.
Has anyone had a solution for this?

Hi @CSKH_ePlus,

I don’t think it’s currently possible to do this, based on the different “MongoDB Actions” I see in the doc: Sign in to GitHub · GitHub (left panel).

If you know the names of the collections, I guess it wouldn’t be a problem to loop through them with JS.

Cheers,
Maxime.

The question is a little vague.

It’s pretty straight forward to loop (iterate) over a collection but we would need to know what your platform is and what kind of data you’re after. Generally iterating over a large quantity of data can be a inefficient but again, more details would enable us to assist you.

Can you update the question with more information? What do your objects look like? What kind of data are you after? Examples?

I guess the OP wants to do something like this, but inside a Realm Function.

My JS script executed in mongosh.

db.a.drop();
db.b.drop();
db.c.drop();

db.a.insertOne({a:1});
db.b.insertOne({b:1});
db.c.insertOne({c:1});

let colls = db.getCollectionNames();
print(colls);

for (let coll of colls) {
  printjson(db.getCollection(coll).find().toArray());
}

Mongosh output:

> load("test.js")
[ 'a', 'b', 'c' ]
[ { _id: ObjectId("60be5a4b457aaba0111734ca"), a: 1 } ]
[ { _id: ObjectId("60be5a4b457aaba0111734cb"), b: 1 } ]
[ { _id: ObjectId("60be5a4b457aaba0111734cc"), c: 1 } ]

There isn’t, to my knowledge, an equivalent of getCollectionNames() in Realm Functions that would allow this kind of algo.

Cheers,
Max.