0

So I'm trying to find all my documents that hasn't images object, and also count them.

Find within a cursor goes well, as I iterate on the cursor itself so I don't need to wait for fetching, but count takes forever.

I have 11M (11,000,000) records on that collection, it doesn't has any index but _id so I know I need to create indexes, but I don't know which indexes I would need...

My document structure has integers and strings, but it contains an Objectwhich is images.

name (str) | title (str) | images (Object)

The images object has this structure

red (int) | green (int) | blue (int) | url (str) | filename (str) | path (str) | product (str)

And my query is simple

db.products.find({'images': {'exists': false }}).count()

I've also tried

db.products.count({'images': {'exists': false }})

but as I say, it takes forever to count...

1 Answer 1

3

Have you tried to index images field?

db.products.ensureIndex({'images':1})

Nested images fields can be accessed using .(dot) notation

Sign up to request clarification or add additional context in comments.

4 Comments

I haven't, if I do that It will block DB? I need to run it in background, right?
By the way the question was to know the appropiate way to create an index for a nested object
@RobertW.Hunter: you might want to try a solution before complaining. On a test instance, of course. There might be a reason for the answer, albeit not elaborated. ;)
I didn't complain, sorry if it sound like that. Using that index doesn't solve anything, the count query when a filter is applied never ends, is so slow that never ends...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.