如何使用mongotemplate更新mongodb中的内部/嵌入文档

时间:2015-02-04 05:21:43

标签: mongodb spring-data-mongodb

有人可以帮我写一个代码来更新“坐标”。我能够更新地址而不是坐标。

{ 
    "_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" 
    } 
}

我的代码是这样的:当我更新地址时它正在工作,但我无法更新坐标。

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 个答案:

答案 0 :(得分: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);