如何使用 mongotemplate 更新 mongodb 中的内部/嵌入文档 [英] How to update a inner/embedded document in a mongodb using mongotemplate

查看:503
本文介绍了如何使用 mongotemplate 更新 mongodb 中的内部/嵌入文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我写一个代码来更新坐标".我能够更新地址,但不能更新坐标.

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;
}

推荐答案

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);

这篇关于如何使用 mongotemplate 更新 mongodb 中的内部/嵌入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆