How to query an Array of Subdocuments via BSON (MongoDB Stitch Android)

21 views
Skip to first unread message
Message has been deleted

Sophia Mudaliyar

unread,
Jan 31, 2020, 7:56:49 AM1/31/20
to BSON
I have a cart collection which has the schema:
{
      "_id":
 {"$oid":"-------"},
"user_id":"-------",
"items":
   [
           {
      "itemname":"Socks",
        "itemcost":{"$numberInt":"50"}
       },
       {
       "itemname":"Cycle Bell",
        "itemcost":{"$numberInt":"849"}
       }
    ],
"location":"pune",
"date":"2020-01-05",
"time":"22:22:22",
"username":"sophia",
"checked":false
}
I want to access and print "ítemname", "itemcost" contained inside array "items".
Query for getting the contents from database should be written in CartItem.java which is of BSON format.

How to get the values of subdocuments by writting the query in CartItem.java (into fromBsonDocument ) using getArray() or any suitable method?
 
 My Code Snippet:

  static CartItem fromBsonDocument(final BsonDocument doc) {
       return new CartItem(
               doc.getObjectId("_id").getValue(),
               doc.getString("user_id").getValue(),
               doc.getDocument("store_details").getString("name").getValue(),
               doc.getDocument("store_details").getString("location").getValue(),

                doc. _____________________
               /* This is where i need help ......i am not able to get
                   the values inside itemname, itemcost which i need to print in cardview?
                 Can someone help with declaration part as well as this underlined part?
                  */

                doc.getString("date").getValue(),
               doc.getString("time").getValue(),
               doc.getString("username").getValue()
       );
   }

Reply all
Reply to author
Forward
0 new messages