The document formats stored in MongoDB are as follows, implementing queries Fromdata data for the specified values under did and Dvid
{ "_id": { "$oid":"553f4a9facc494278f5ad1b0" }, "RID":"9eec7fbe8a6f4d17b02756fdbcefcc41", "Rname":"Test Scenario 1", "username":"zhaoptest", "Enable":1, "Fromdata": [ { " did":"AAA", "Dvid":"1", "opt":"3", "value":" -" }, { " did":"BBB", "Dvid":"1", "opt":"3", "value":" -" } ], "Todata": [ { " did":"AAA", "Dvid":"2", "value":"3" }, { " did":"CCC", "Dvid":"2", "value":"3" } ]}
Use Db.device_rules.find ({"Fromdata.did": "BBB", "Fromdata.dvid": "1"}) to query did=bbb,dvid=1 data in Fromdata
But when the query did=bbb,dvid=2, can still detect the above results, query conditions and the different documents in the array to match!
Using the conditional operator "$elemMatch" can combine a set of conditions and also achieve the effect of a "dot notation" fuzzy query, as follows:
Db.device_rules.find ({"Fromdata": {"$elemMatch": {"did": "BBB", "Dvid": "2"}})
To query using Morphia:
this. CreateQuery (). Field ("Fromdata.did"). Equal (Fromdid). Field ("Fromdata.dvid"). Equal (Fromdvid). aslist ();
MongoDB Query Inline Document