I have the following doc definition (it's ruby)
class Block
include Mongoid::Document
field :index, type: Integer # index 0,1,..
field :codes, type: Array #[A, B, C,... ]
embedded_in :Video
end
class Video
include Mongoid::Document
field :name, type: String
embeds_many :blocks, :order => :index.asc
end
I want to query matching the property video.blocks.codes, but it is an array property of an embedded doc. I mainly want to do two types of queries:
- How many blocks exist with a non-null/non-empty
codesarray? - How many blocks exist where the codes array matches a certain string in a given position?
Here's an example of the data I'm trying to match:
video#1
blocks: [{index: 1, codes:["a","g","c"]}, {index: 2, codes: [] }]
video#2
blocks: [{index: 1, codes:["x","b","d", "e"]}, {index: 2, codes: ["x","b"] }]
For example, I want to know how many blocks are there without non-empty codes array (answer is three blocks), and how many blocks are there with a b in the second position(index 1) (answer is there are two).
I'm using the mongoid driver so ideally the query would use the driver, but plain mongo is fine. Thanks!