Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isEmpty to cursors #1504

Closed
mitar opened this issue Oct 15, 2013 · 15 comments
Closed

Add isEmpty to cursors #1504

mitar opened this issue Oct 15, 2013 · 15 comments

Comments

@mitar
Copy link
Contributor

mitar commented Oct 15, 2013

Please provide a isEmpty method on cursors. Currently there are two ways to do it: use count() or fetch one element and see what you get. But isEmpty could probably be much simpler and faster than any of those. Counting exact count just to know if there is any element is not the best either.

@ankurp
Copy link

ankurp commented Jan 26, 2014

What do you exactly mean by isEmpty method on cursors? What cursors? Do you want to check if element has any children?

@awatson1978
Copy link
Contributor

+1
isEmpty would be handy to have around. And also maybe indexOf() and next().

@ankurp - Mitar is talking about the minimongo cursors defined by the Collections API.

@ankurp
Copy link

ankurp commented Jan 26, 2014

@awatson1978 Thanks for clarifying. Let me take a look at it as it seems something I can add.

@mitar
Copy link
Contributor Author

mitar commented Feb 17, 2014

OK, but this is needed to do it performance wise. Current approach (calling count or fetching one document) is simply too much work to just know if collection is empty or not. This is not just a syntactic sugar because it cannot be implemented without support in Meteor.

@Slava
Copy link
Contributor

Slava commented Feb 17, 2014

@mitar doesn't cursor.findOne() accomplish the job in a fairly optimal way?

@glasser
Copy link
Contributor

glasser commented Feb 17, 2014

How do you think that the framework would implement "isEmpty" other than as "findOne, no fields"?

@mitar
Copy link
Contributor Author

mitar commented Feb 17, 2014

MongoDB has hasNext call. I think this would be best to use it? If it hasNext initially, it is not empty.

@mitar
Copy link
Contributor Author

mitar commented Feb 17, 2014

For findOne you need both to set fields to none and disable any possibly defined transformation. Additionally, it is also question how reactivity is then applied. You would want that reactivity is triggered only when empty state changes and not necessary when first document changes or when count changes.

@glasser
Copy link
Contributor

glasser commented Feb 17, 2014

hasNext is not part of the Node driver interface. And I'm reasonably sure that as far as efficiency goes, it's equivalent to findOne.

@mitar
Copy link
Contributor Author

mitar commented Feb 18, 2014

Hm, you are right.

@jagill
Copy link
Contributor

jagill commented Feb 19, 2014

@glasser I can't find the syntax for "no fields" in the meteor documentation or the mongo documentation. Naive attempts like {fields:null} or {fields:{}} don't work, of course. This is topical because I'm getting an infinite autorun loop caused by the reactivity associated with the extra fields on an object I get by findOne. There are hackier ways around it, of course, but "no fields" (or isEmpty) sure would be nice!

@Slava
Copy link
Contributor

Slava commented Feb 19, 2014

@jagill cursor.findOne({}, {fields: {_id: 1}}).

@jagill
Copy link
Contributor

jagill commented Feb 19, 2014

@Slava that's what I've been doing, but reactivity will be triggered if findOne finds a different document, right?

I think that's an acceptable cost most of the time, tho.

@Slava
Copy link
Contributor

Slava commented Feb 19, 2014

@jagill right, it can retrigger if it finds a different document, however it is easy to work-around this behavior if you store the previous result of !!cursor.findOne({}, {fields:{_id:1}}) and compare it on every invocation.

@jagill
Copy link
Contributor

jagill commented Feb 19, 2014

@Slava cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants