MongoDB和upsert问题 [英] MongoDB and upsert issue

查看:91
本文介绍了MongoDB和upsert问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型:

1-ResourceVacation:

1-ResourceVacation:

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

2-假期:

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

插入后的ResourceVacation的JSON:

The JSON for the ResourceVacation after I inserted into it:

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

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

我需要增加一个假期;如果我需要的resourceId已经存在于ResourceVacation中,请将"vacationList"替换为我拥有的json.

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: 插入一个新的ResourceVacation

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

我收到以下异常:

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

推荐答案

首先,看起来您根本没有在做upsert. Java API中的语法将db.update的第三个参数设置为true.

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 */)

您也不应该执行$ push-在mongo shell中您要执行的操作的语义是:

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)

这说:如果存在具有resourceID的resourceVacation,则将其作为"vacationList"提供给我.如果不存在,则插入该记录.

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.

如果您直接使用Java API,则等同于上述条件即可.

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

好像您正在使用Spring的MongoTemplate.您将需要检查使用的是哪个版本,因为它没有用于允许 upserts .该问题已标记为已解决.如果您使用的是旧版本,则有一种解决方法,描述了此处.

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.

如果您是最新的,则应该能够直接使用新添加的upsert方法,如

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

这篇关于MongoDB和upsert问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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