数组我有JSON这样的:更新使用MongoDB的
{ "_id" : "1", "_class" : "com.model.Test", "itemList" : [ { "itemID" : "1", "itemName" : "Foo", "resources" : [ { "resourceID" : "1", "resourceName" : "Foo Test1", }, { "resourceID" : "2", "resourceName" : "Foo Test2", } ] } ] }
我需要能够更新的资源列表。 我也做了以下内容:
BasicDBObject updateQuery = new BasicDBObject();
updateQuery.put("id", "1");
BasicDBObject updateCommand = new BasicDBObject();
List<Resource> resources = populateResources();//Fetch a new list of Resources
updateCommand.put("$push", new BasicDBObject("resources", resources));
MongoOperations mongoOperations = mongoConfiguration.getMongoTemplate();
DBCollection db = mongoOperations.getCollection("myCollection");
db.save(updateCommand);
我得到follwoing错误:
java.lang.IllegalArgumentException: fields stored in the db can't start with '$' (Bad Key: '$push')
当我用:
db.update(updateQuery, updateCommand, true, true);
我有以下异常:
java.lang.IllegalArgumentException: can't serialize class com.model.Test
我试过了: db.updateMulti(updateQuery,updateCommand); 我没有得到任何例外,也没有任何更新发生在文件中。
那么我在这里错过了什么!
谢谢Marc.That非常有帮助。 – Echo 2012-04-10 17:59:29