How to update a inner/embedded document in a mongodb using mongotemplate

a 夏天 提交于 2019-12-12 04:54:32

问题


Can some one help me to write a code to update "coordinates". I was able to update the address but not the coordinates.

{ 
    "_id": "2c9080e54b4ee7ac014b4ee8e5100000", 
    "_class": "com.myparking.dataservice.mongodb.documents.ParkingSiteDocument", 
    "address": { 
        "streetAddress": "bellandur",
        "locality": "ORR", 
        "region": "bangalore", 
        "country": "india", 
        "postalCode": "560102" 
    }, 
    "geoLocation": { 
        "coordinates": [ 12.934292, 77.680215 ], 
        "type": "Point" 
    } 
}

My code goes like this: When I update the address it is working but I am not able to update the coordinates.

public ParkingSiteDocument updateParkingSite(final ParkingSpaceDTO pSpace) {
    ParkingSiteDocument parkingSpace = null;
    try{
        // If the collection doesn't exist return.
        if (!mongoTemplate.collectionExists(ParkingSiteDocument.class)) {
            // return.
            return null;
        }           
        Query query = new Query();
        // query to fetch the parking site based on the id.
        query.addCriteria(Criteria.where("pSiteId").is(pSpace.getpSpaceId()));
        parkingSpace =  mongoTemplate.findOne(query, ParkingSiteDocument.class);
        // If the parking space is not available return;
        if(parkingSpace == null) {
            return null;
        }
        // Update address and coordinates
        Update update = new Update();
        // Updating the address.
        if(pSpace.getAddress() != null) {
            Address newAddress = new Address();
            newAddress.setCountry(pSpace.getAddress().getCountry());
            newAddress.setLocality(pSpace.getAddress().getLocality());
            newAddress.setPostalCode(pSpace.getAddress().getPostalCode());
            newAddress.setRegion(pSpace.getAddress().getRegion());
            newAddress.setStreetAddress(pSpace.getAddress().getStreetAddress());
            // converting it into mongo document.
            MongoConverter converter = mongoTemplate.getConverter();
            DBObject newRec = (DBObject) converter.convertToMongoType(newAddress);
            update.set("address", newRec);
        }
        // Update the geolocation coordinates
        if(pSpace.getGeoCoordinates() != null) {
            // creating new coordinates from the input DTO.
            Double[] coordinates = new Double[]{pSpace.getGeoCoordinates().getLongitude(), 
                                     pSpace.getGeoCoordinates().getLatitude()};
            MongoConverter converter = mongoTemplate.getConverter();
            DBObject newRec = (DBObject) converter.convertToMongoType(coordinates);
            update.set("geoLocation.coordinates", newRec);
        }
        // update query.
        mongoTemplate.updateFirst(query, update, ParkingSiteDocument.class);
    } catch(Exception e) {
        logger.error(this.getClass().getSimpleName(), "updateParkingSite | Exception" + e.getMessage());
    }
    return parkingSpace;
}

回答1:


if (!mongoTemplate.collectionExists(ParkingSiteDocument.class))//or document name

        mongoTemplate.createCollection(ParkingSiteDocument.class);//or document name

    DBCollection db=mongoTemplate.getCollection(ParkingSiteDocument.class);//document name

    BasicDBObject updateDocument = new BasicDBObject();

    DBObject update = new BasicDBObject("$set",
                       new BasicDBObject("geoLocation",
                        new BasicDBObject("coordinates", "12.934292,77.680215")));

    BasicDBObject searchQuery= new BasicDBObject().append("_id", new ObjectId("54d1d939e4b044860afcdf6d"));

    db.update(searchQuery, update);


来源:https://stackoverflow.com/questions/28314043/how-to-update-a-inner-embedded-document-in-a-mongodb-using-mongotemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!