MongoDB and upsert issue

本小妞迷上赌 提交于 2019-12-22 13:31:53

问题


I have two models:

1-ResourceVacation:

    @Id
    private String resourceID;
    private List<Vacation> vacationList;

2-Vacation:

@Id
private String id;
private String start;
private String title;

The JSON for the ResourceVacation after I inserted into it:

{ "_id" : "foo", "_class" : "com.test.model.ResourceVacation", "vacationList" : [ { "_id" : "1", "start" : "abc", "title" : "test" } ] }

I need to upsert a vacation;if the resourceId I need is already existed into the ResourceVacation,replace the "vacationList" into the json with the one I have.

ELSE: Insert a new ResourceVacation

    Vacation vacation = new Vacation("a", "a", "a");
    List<Vacation> list = new ArrayList<Vacation>();
    list.add(vacation);

    ResourceVacation resourceVacation = new ResourceVacation("foo", list);
    MongoOperations mongoOperations = mongoConfiguration.getMongoTemplate();
    DBCollection db = mongoOperations.getCollection("resourceVacation");

    BasicDBObject myQuery = new BasicDBObject("_id", resourceVacation.getResourceID());
    BasicDBObject myUpdate = new BasicDBObject("push ", new BasicDBObject("vacationList",
            resourceVacation.getVacationList()));

    db.update(myQuery, myUpdate);

I get the following exception :

java.lang.IllegalArgumentException: can't serialize class com.springway.model.Vacation

回答1:


First, it doesn't look like you are doing an upsert at all. The syntax for that in Java API would have a third argument to db.update set to true.

   db.update(com.mongodb.DBObject, com.mongodb.DBObject, boolean /*upsert */, boolean /* multi */)

You shouldn't be doing a $push either - the semantics of what you say you want to do in mongo shell would be:

db.collection.update( {"resourceVacation":resourceID}, {$set:{"vacationList":[...]}, true)

This says: if resourceVacation having resourceID exists, then make its "vacationList" what I'm giving you. If it doesn't exist then insert this record.

If you were using Java API directly, the equivalent of the above would be sufficient.

Looks like you are using MongoTemplate from Spring. You will need to check what version of it you are using because it didn't use to allow upserts. That issue is marked as resolved though. If you're stuck on the older version, then there is a workaround described here.

If you are on the latest, you should be able to use the newly added upsert method directly, as described here.




回答2:


The exception could be caused by a known bug with the updateFirst method. Take a look here.



来源:https://stackoverflow.com/questions/10490205/mongodb-and-upsert-issue

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