Does anyone know if there any intention in the near future to support mongo;s cursor.next() function in server code and minimongo?
Technically, it is supported as it is built into the driver: meteor/packages/mongo/asynchronous_cursor.js at 576f7485a2050c206abb09e0f689fb57dd86167d · meteor/meteor · GitHub
What would be your use case for it?
Assume I have a collection where I want to find all the docs that satisfy a complex test function of my own design:
My code would likely look something simple like:
let cursor = collection.find();
cursor.forEach((doc) => {
if (complexTest(doc))
console.log(doc);
}
That is straightforward. It runs through all the docs.
But instead if I want to test if ANY document in my collection satisfies that condition, i would like to do the following:
let cursor = collection.find();
let found = false;
while (cursor.hasNext()) {
let doc = cursor.next();
if (complexTest(doc)) {
found = true; break;
}
console.log(found)
This would only fetch docs until it finds a single document that matches my condition.
Without hasNext() && next(), i cannot see how to code this function without fetching all the documents
If I use fetch().any(), fetch() loads all the documents, and
using foreach() loads the documents one at a time but has no break functionality, so it too loads all the documents.
hasNext() and next() would improve my code performace, especially in large collections, since it would only load the documents that I need in order to do my test.
I guess you can use findOneAsync()
, but if you really want to use low level API if mongodb then you can use rawCollection()
instead.
i’m not sure how findOneAsync() would help me in this case.
Ok, like @minhna said, rawCollection() is the equivalent of native MongoDB Driver. I wonder if your complex test could be written as an aggregation. In theory, everything could be written as an aggregation and send the query to mongo rather than iterating over documents one by one.
Would you please try to use AI to convert your function into a Mongo Aggregation. I am really currious how that would work out.
I will do that.
BTW, the ‘complex function’ i presented is simplified compared to my actual test. My actual ‘complex function’ examines two unique collections and their relationship to each other to come to a resolution, but usually resolves after examining only about 4 unique records each of the two collection of about 15,000. records Next would be very useful for both clarity and speed, instead of loading 15000 records that are ignored.
You don’t really need next since the cursor returned from find exposes an iterator that you can step through using its next method.
You get it by simply indexing the cursor with [Symbol.asyncIterator]
This is all well documented in the meteor/mongo typescript types.
Thanks. I will investigate it, though I don’t use typescript. I’ll look deeper into the cursor object to see what I can find.