使用 mongotemplate 滚动更新 mongodb 中的现有记录 [英] Rolling around to update existing record in mongodb using mongotemplate

查看:57
本文介绍了使用 mongotemplate 滚动更新 mongodb 中的现有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,我想使用 spring mongotemplate 更新 mongodb 中的现有记录.为此,我必须从 mongodb 中搜索现有记录.在 mongodb 中保存记录时,它会自动创建名为 "_id" 在每条记录中.我尝试了很多来访问此字段.但我无法做到.. :(

For me i want to update the existing record in mongodb using spring mongotemplate.For that i have to search the existing record from the mongodb.While saving record in mongodb it automatically creates field called "_id" in each and every record. I tried a lot to access this field.But i can't able to do.. :(

所以我在我的 DTO 对象中手动创建了一个名为 myid 的字段.每次保存时,我都会序列化文件中的 id 并保存.使用这个 myid 搜索.我知道硬代码文件路径会在服务器部署时产生问题.

So that i manually created one field called myid in my DTO object. Each and every time saving i'm serializing the id in file and saving.Searching using this myid.I know hard code file path will create problem while deploying in server.

有什么方法可以做到这一点,或者有什么方法可以访问系统生成的_id 以进行搜索和更新.. ??

Is there any way to do this or any way to access system generated _id for searching and updating .. ??

谢谢.

推荐答案

_id 是主键.如果你没有在你的对象中指定一个带有注释 @id 的字段,那么 Mongo 会为你创建一个.您无法访问它,因为 Mongo 无法填充该字段,因为它不存在或缺少映射.

_id is a primary key. If you don't specify a field in your object with annotation @id then Mongo will create one for you. You cannot access it because Mongo is not able to populate that field as it does not exists or there is a mapping missing.

你的代码中必须有类似的东西[如果你也能发布你的代码就好了.]

You will have to have something like in your code [It would be nice if you could post you code too.]

import org.springframework.data.annotation.Id;

@Id
protected String id;

HTH

这篇关于使用 mongotemplate 滚动更新 mongodb 中的现有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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