Mongo Query on Subfields
mongodb
Solution
Another, more efficient, approach would be to implement your "status" sub-document as an array of "typed values", like this:
{"_id" : ObjectId("51385d2308d427ce306f0100"),
"aid" : "1",
"studyId" : "study-1",
"mediaType" : "microBlog",
"text" : "bla bla",
"sentences" : "bla bla",
"status" : [
{ type: "algo1", value: "required" },
{ type: "algo2", value: "required" },
{ type: "algo3", value: "completed" },
{ type: "algo4", value: "completed" }
],
"priority" : "u"}
This would allow you to find all the documents, for which any of the sub-field has value "required", with this query:
db.foo.find({"status.value":"required"})
Defining an index on this sub-field would speed up the query:
db.foo.ensureIndex({"status.value":1})
Problem
I have a document - ``` {"_id" : ObjectId("51385d2308d427ce306f0100"), "aid" : "1", "studyId" : "study-1", "mediaType" : "microBlog", "text" : "bla bla", "sentences" : "bla bla", "status" : { "algo1" : "required", "algo2" : "required", "algo3" : "completed", "algo4" : "completed" }, "priority" : "u"} ``` The status field has multiple sub-fields with different status values. Is it possible to create a query such that it returns all documents for which any of the status sub-field's value is "required"? Something like `db.foo.find({status : "required"})` which would give me all documents for which any of the sub-field has value "required"