简体   繁体   中英

Updating nested embedded array document in MongoDB using java driver 3.2

I have this document in MongoDB 3.2. I am unable to update my nested array document

{
    "_id" : ObjectId("5761c22edd93e211f49d5d51"),
    "fullName" : "Mohammad Amir",
    "enroll" : "abc123",
    "email" : "amir.fragrance@gmail.com",
    "mobile" : "8090370605",
    "password" : "$2a$12$RTSx6T8SyY9d8d16HpgatuEUHwRBi60PZslCWvSGojNJSx21HKQuK",
    "registeredOn" : ISODate("2016-06-15T21:01:34.736Z"),
    "profile" : {
        "isCompleted" : true,
        "verification" : [ 
            {
                "provost" : false,
                "verifiedBy" : null,
                "verifiedOn" : null
            }, 
            {
                "chairman" : false,
                "verifiedBy" : null,
                "verifiedOn" : null
            }
        ],
        "isChecked" : false,
        "fullName" : "Mohammad Amir",
        "enroll" : "abc123",
        "fatherName" : "jhgjgjh",
        "motherName" : "kghjhgj",
        "dob" : "2016-06-14T18:30:00.000Z",
        "gender" : "Male",
        "address" : "ytiutit",
        "city" : "Abdul",
        "state" : "Andaman and Nicobar Islands",
        "country" : "Afghanistan",
        "pincode" : "76575",
        "courseType" : "UG",
        "courseName" : "Adv Dip in Int.Decoration",
        "semesterYear" : 1,
        "facultyNumber" : "765gvjn",
        "department" : "Agricultural Eco. Business Mngt.",
        "hall" : "Abdullah Hall",
        "verification[0]" : {
            "provost" : true,
            "verifiedBy" : "Director Computer Centre",
            "verifiedOn" : ISODate("2016-06-16T19:37:33.161Z")
        }
    },
    "contact" : {
        "emailID" : {
            "isVerified" : false,
            "isChecked" : false
        },
        "mobileID" : {
            "isVerified" : false,
            "isChecked" : false
        }
    }
}

I want to update it like this

StringBuilder sb = new StringBuilder();
        BufferedReader br = request.getReader();
        String str = null;
        while ((str = br.readLine()) != null) {
            sb.append(str);
        }
        JSONObject jObj = new JSONObject(sb.toString());
        ObjectId _id = new ObjectId(jObj.getString("_id"));
        boolean value = jObj.getBoolean("value");
        String mode = jObj.getString("mode");
        String verifiedBy = jObj.getString("verifiedBy");

        InputStream input = getServletContext().getResourceAsStream("/WEB-INF/config/config.properties");
        Properties properties = new Properties();
        properties.load(input);
        String host = properties.getProperty("host");
        int port = Integer.parseInt(properties.getProperty("port"));
        MongoClient mongoClient = new MongoClient(host, port);
        MongoDatabase db = mongoClient.getDatabase("admin");

        MongoCollection<Document> coll = db.getCollection("students");
        Bson where = new Document().append("_id", _id);
        Bson update = new Document()
                .append("profile.verification[0].provost", value)
                .append("profile.verification[0].verifiedBy", verifiedBy)
                .append("profile.verification[0].verifiedOn", new Date());
        Bson set = new Document().append("$set", update);
        try {
            coll.updateOne(where, set, new UpdateOptions().upsert(true));
            List<Document> documents = (List<Document>) coll.find(where).into(new ArrayList<Document>());
            JSON json = new JSON();
            String serialize = json.serialize(documents);
            response.getWriter().write(serialize);
        } catch (MongoWriteException e) {
            JSONObject json = new JSONObject();
            json.append("success", false);
            json.append("class", "alert alert-success col-sm-8");
            json.append("message", "Something went wrong");
            String res = json.toString();
            response.getWriter().write(res);
        }

There is no error but, profile.verification[0].provost = true for some reason is not being updated.

Any pointers would be appreciated.

You need to access the array element as profile.verification.0.provost etc, not as [0]. You see the effect in your own example, as you added a sub-document "verification[0]" at the end of the profile-part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM