【发布时间】:2013-11-24 05:57:52
【问题描述】:
我是 Spring 框架的新手。我有我的 mongo 文档,比如
{
"_id" : ObjectId("527242d584ae917d8bd75c7b"),
"postTitle" : "Car",
"postDesc" : "rent",
"owner" : ObjectId("526a588f84aed6f41cca10bd"),
"intrest" : []
}
我想要的是搜索具有 id 的文档
"_id" : ObjectId("527242d584ae917d8bd75c7b")
并将其更新为
{
"_id" : ObjectId("527242d584ae917d8bd75c7b"),
"postTitle" : "Car",
"postDesc" : "rent",
"owner" : ObjectId("526a588f84aed6f41cca10bd"),
"intrest" : [
{
"userId" : ObjectId("526a587d84aed6f41cca10bc"),
"timestamp" : ISODate("2013-10-31T11:45:25.256Z")
},
{
"userId" : ObjectId("526a587d84aed6f41cca10bc"),
"timestamp" : ISODate("2013-11-31T11:55:25.256a")
}
]
}
我的域名是
@Document
public class Post {
@Id
private ObjectId _id;
private String postTitle;
private String postDesc;
private ObjectId owner=Global.getCurruser();
private List<Intrest> intrest = new ArrayList<Intrest>();
// Getters and setters
}
@Document
public class Intrest {
private ObjectId userId;
private Date timestamp;
// Getters and setters
}
我应该写什么 upsert 来添加或修改 intrest array[] 中的条目。
请帮忙。
【问题讨论】:
-
你不想做一个 upsert - 你想做一个更新。 Upsert 是当您想要插入新文档时 - 看起来您只是想向现有字段添加新值。
标签: java mongodb spring-mvc spring-data-mongodb