Today in the project encountered about MongoDB nested query, has not been contacted before, query the relevant data to record the results.
The data in the MongoDB are as follows:
> Db.customer.findOne ()
{
"_id": ObjectId ("57636c8e35defe029962107e"),
"_class": " Com.bu2trip.ticket.model.Customer ",
" name ":" Wang ",
" phone ":" 18408221624 ",
" gender ": 1,
" Birthday ":" 1995-7-9 ","
Passport ":" 620524 ",
" Login_user ": {
" _id ": ObjectId (" 5760e593086659036b77c124 "),
" email ":" test@bu2trip.com ",
" phone ":" ""
}
If you want to query the entire inline document, the query statement is as follows:
Db.customer.find ({"Login_user": {"_id": ObjectId ("5760e593086659036b77c124"), "email": "test@bu2trip.com", "Phone": "110"}})
All values with Login_user as keys must be written out in the query criteria.
Queries for only specific key values for inline documents are as follows:
Db.customer.findOne ({"Login_user.phone": "110"})
You only need to match a specific key value in a nested document.
For to Java clients, it is:
has been mongotemplate as an example
Criteria Criatira = new criteria ();
Criatira.andoperator (Criteria.where ("Login_user.phone"). is (a);
Query query = new query (Criatira);
Customer customer = Mongotemplate.findone (query,customer.class);
Loginuser loginuser = new Loginuser (xxxx);
Criteria Criatira = new criteria ();
Criatira.andoperator (Criteria.where ("Login_user"). is (Loginuser));
Query query = new query (Criatira);
Customer customer = Mongotemplate.findone (query,customer.class);