[英]Mongo db will not update an existing Document
I am using java pojo's and spring MongoTemplate, I spent few hours now, trying to understand why MongoDb will not update an existing Document 我正在使用Java Pojo和Spring MongoTemplate,我现在花了几个小时,试图了解MongoDb为什么不更新现有文档
I am able to insert documents, but not to update existing ones 我可以插入文件,但不能更新现有文件
Any simple way to trace this in mongo/spring? 有任何简单的方法可以在mongo / spring中进行跟踪吗?
I tried so far: 到目前为止,我尝试过:
Simply call save: 只需调用保存:
person.setUpdateDate(new Date());
mongoTemplate.save(person);
//not working
call updateFirst 致电updateFirst
mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(person.getId())),
Update.update("objName", "NotWorking"),Person.class);
Get the collection first, convert to DB Object, then find and modify: 首先获取集合,转换为数据库对象,然后查找并修改:
DBCollection coll=mongoTemplate.getCollection("Person");
DBObject mongoPerson = morphia.toDBObject(person);
DBObject afterSave = coll.findAndModify(new BasicDBObject("id", person.getId()), mongoPerson);
//afterSave is null
Try find by _id, then save: 尝试通过_id查找,然后保存:
DBObject mongoPerson = morphia.toDBObject(person);
Person found1 = mongoTemplate.findById(person.getId(), Person.class);
found1.setObjName(person.getName());
mongoTemplate.save(found1);
Find first, then save using "_id": 首先查找,然后使用“ _id”保存:
DBCursor found = coll.find(new BasicDBObject("_id", person.getId()));
DBObject first = found.next();
mongoTemplate.save(first);
Find first, then save using "id": 首先查找,然后使用“ id”保存:
DBCursor found = coll.find(new BasicDBObject("id", person.getId()));
DBObject first = found.next();
mongoTemplate.save(first);
Try find and modify then save: 尝试查找并修改,然后保存:
DBObject afterSave = coll.findAndModify(new BasicDBObject("id", person.getId()), mongoPerson);
//afterSave is null
mongoTemplate.save(person);
Update individual fields using query: 使用查询更新单个字段:
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(person.getId()));
mongoTemplate.updateFirst(query, new Update().set("name",person.getName())
.set("desc", person.getDesc())
.set("updateDate",new Date())
,
Person.class)
*Update this is also working: *更新也可以:
mongoOperations.save(person,"Person");
Using this article I was able to make update work - not the way I want, but...It works... 通过这篇文章,我能够使更新正常工作-不是我想要的方式,但是...有效...
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.core.query.Update.update;
@Controller
public class PersonController {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
MongoOperations mongoOperations;
...
mongoOperations.updateFirst(query(where("_id").is(person.getId())), Update.update("name", newPersonName),"Person");
Not sure yet why this work while 尚不知道为什么这会在
mongoOperations.save(person);
Is not working... 不管用...
You need to add db name in methods such as: 您需要在以下方法中添加数据库名称:
public class UserInfoService{
public static final String INFO = "info";
public void someMethod(UserInfo userinfo){
mongo.save(userinfo, INFO);
}
if u dont, mongo will save ur data in db with name userInfo (with capital 'I' ). 如果您不这样做,mongo会将您的数据保存在db中,其名称为userInfo(大写的为'I')。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.