query filter for mongodb java driver

1.6k views Asked by At

consider the following documnet structure:

 {
 "_id" : <ObjectId>,
 "name" : <string>,
 "contact" : {
    "phone" : <string>
    "email" : <string>
    "location" : [ <longitude>, <latitude> ]
  },
  "stars" : int,
   "categories" : <array of strings>
  "grades" : <array of integers>,
   }

according to mongodb website the following query specify

the stars field is greater than or equal to 2 and less than 5, AND the categories field equals "Bakery" (or if categories is an array, contains the string "Bakery" as an element):

collection.find(
    new Document("stars", new Document("$gte", 2)
         .append("$lt", 5))
         .append("categories", "Bakery")).forEach(printBlock);
  1. can someone explain me the structure of the query?
  2. why a new document is created (new Document("$gte", 2)?
1

There are 1 answers

0
Minh Tuan Nguyen On

The query looks like this:

{"stars"      : {$gte : 2, $lt : 5}
 "categories" : "Bakery"}

new Document("$gte", 2) has been invoked to create the inner json: {$gte : 2, $lt : 5}