0

I tried to use length on an array that is within another array and got an error:

db.quotes.find( { $where: "this.booksWithQuote.authors.length>0" } ) 
// fails with this.booksWithQuote.authors is undefined

I'm basing this syntax on this example MongoDB – find all documents where an array / list size is greater than N that works:

db.domain.find( {booksWithQuote: {$exists:true}, $where:'this.booksWithQuote.length>0'} )
// Above works

So I'm wondering if this is possible, how can I find all documents that have array1.array2 where array2 length is greater than zero.

I've tried the following but it fails with a syntax error:

db.quotes.find( { 
  "booksWithQuote": {$exists: true} },
  "booksWithQuote.authors": {$exists: true} },
  $where: "booksWithQuote.authors.length>0" } ) 
// fails with this.booksWithQuote.authors is undefined

It's worth pointing out that if I knew the author of a book, the nested array with array searching worked. Pretty cool!

db.quotes.find( { "booksWithQuote.authors" : "Sam Fisher" } ) 
// Returns all quotes that have a book that has the author Sam Fisher

But in my case I'm just trying to find all of the quotes that have more than one author on any given book.

To follow along, consider this example.

I have a collection of quotes, and each quote has a list of books where the quote was used, and each book has an array of authors.

Below is some sample data so you can understand the structure of the data. It shows one quote with no books, another quote with a book but no authors, and a third quote with books and several authors.

[ { quoteText: "Love to code", booksWithQuote: [ ] }, { quoteText: "Another Quotesake", booksWithQuote: [ { title: "Where is elmo", authors: [ ] } ] }, { quoteText: "For goodness sake", booksWithQuote: [ { title: "The search for Elmo", authors: [ "John Smith", "Sam Fisher", "Jim Wiggins" ] }, { title: "Finding Elmo", authors: [ "Sam Fisher" ] }, { title: "Mercy me", authors: [ ] } ] } ]

So to reiterate, How can I find all documents that have an array within another array where the 2nd array has one or more elements?

2 Answers 2

5

You can use $exists operator and refer to first element of an array using dot notation:

db.col.find({ "booksWithQuote.authors.0": { $exists: true } })
Sign up to request clarification or add additional context in comments.

1 Comment

Great, this worked! Just FYI. I was going to ask, is it possible to see if there are two or more authors, and tried this booksWithQuote.authors.1 and it worked! Bonus for me.
0

MongoDB documentation: https://www.mongodb.com/docs/manual/reference/operator/query/expr/

you can use $expr to achieve this

$expr:{$gte: [{$size: "$selectedArray"}, size]}

Comments

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.