我正在尝试使用Spring Data MongoDB更新mongodb中嵌入文档中的数组字段。
我想在mongodb集合中更新/插入的文档结构如下。
根据部门的每种类型,可以有更多类似的文件,例如"销售","营销"等等。
{
"timestamp": "2014-09-26T04:00:00.000Z",
"department": "accounts",
"employee": [
{
"type": "regular",
"names": [
"Raj",
"Kumar",
"Shankar"
]
},
{
"type": "contract",
"names": [
"Penny",
"Sheldon",
"bob"
]
},
{
"type": "temp",
"names": [
"jerry",
"kramer",
"bubbleboy"
]
}
]
}
基本上,我的更新查询如下,
db.getCollection('mytest').update(
{
"timestamp" : "2014-09-26T04:00:00.000Z",
"department" : "accounts",
"employee.type" : "regular"
},
{ $addToSet: { "employee.$.names" : "Jo" } },
{
upsert: true
}
)
我添加了upsert:true,因为如果没有匹配查询的文档,我想将文档插入到mytest集合中。
当我从mongo shell执行相同操作时,我收到以下错误。
The positional operator did not find the match needed from the query. Unexpanded update: employee.$.names
即使这样有效,我也不确定我们是否有类似的支持在Spring Data mongodb中实现相同的功能。
另外,我的另一个问题是,如果我想为多个部门添加/更新员工,请说" accounts"以及" sales",似乎我必须执行相同的查询,使用不同的值作为我想要更新的部门数(如果不存在插入)。
是否有更好,更高效的选项,如批量/批处理,我可以在单个mongo更新查询中同时更新/插入多个部门的员工。另外,在弹簧数据中是否支持mongodb / mongotemplate。
答案 0 :(得分:1)
首先,我应该说,由于文档结构具有嵌入式数组,因此需要处理多个场景。如果可能,请尝试重新设计文档结构。
对错误的简短回答: - 如果查询找不到匹配的文档,则会出现位置运算符错误。
这就是为什么在我的下面的解决方案中,我已经处理了catch块中的场景。如果你可以通过单元测试,你应该能够理解。
<强>解决方案: - 强>
public Boolean updateDepartmentCollectionWithoutFind(String name, String timeStamp)
throws JsonParseException, JsonMappingException, IOException {
MongoOperations mongoOperations = getMongoConnection();
Query query = new Query();
query.addCriteria(Criteria.where("timestamp").is(timeStamp).and("department").is("accounts")
.and("employee.type").is("regular"));
Update update = new Update();
update.addToSet("employee.$.names", name);
try {
System.out.println(query.toString());
System.out.println(update.toString());
WriteResult writeResult = mongoOperations.upsert(query, update, DepartmentCollection.class);
if (writeResult != null) {
System.out.println("111111111111111111 Update with position parameter has been successful :"
+ writeResult.toString());
}
} catch (DataIntegrityViolationException die) {
System.out.println("Update failed ====>" + die.getMessage());
System.out.println("Trying to update without position parameters ...");
Update updateWithoutPositionOperator = new Update();
updateWithoutPositionOperator.addToSet("employee.names", name);
WriteResult writeResultUpsert = mongoOperations.upsert(query, updateWithoutPositionOperator,
DepartmentCollection.class);
if (writeResultUpsert != null) {
System.out.println("2222222222222222222 Update without position parameter has been successful :"
+ writeResultUpsert.toString());
}
}
return true;
}
我的连接方法供参考: - 如果您有弹簧上下文并且可以根据您的上下文更改上述代码,那么您不需要这样做。
@SuppressWarnings("resource")
public MongoOperations getMongoConnection() {
return (MongoOperations) new AnnotationConfigApplicationContext(SpringMongoConfig.class)
.getBean("mongoTemplate");
}
单元测试: - 我发布了单元测试,因为您需要了解方案和解决方案。请阅读每个测试和方法名称中的注释,以了解在每个方案中执行哪个upsert。
测试3 是一种特殊行为。请仔细看看。
@Test
public void updateDepartmentCollectionWhenNoDocumentsPresent() throws JsonParseException, JsonMappingException, IOException {
//Second upsert executed (i.e. without positional parameter)
Assert.isTrue(queryOperations.updateDepartmentCollectionWithoutFind("Raj", "2014-09-26T04:00:00.000Z"));
}
@Test
public void updateDCWhenMatchingDocumentsPresentButDocumentHasOnlyRegularEmployeeType() throws JsonParseException, JsonMappingException, IOException {
//Second upsert executed (i.e. without positional parameter)
Assert.isTrue(queryOperations.updateDepartmentCollectionWithoutFind("Jo", "2014-09-26T04:00:00.000Z"));
}
@Test
public void updateDCWhenMultipleTypesPresentButNoRegular() throws JsonParseException, JsonMappingException, IOException {
//First upsert executed (i.e. with positional parameter). However, it creates a new document.
Assert.isTrue(queryOperations.updateDepartmentCollectionWithoutFind("Jo", "2014-09-27T04:00:00.000Z"));
}
@Test
public void updateDCWhenMultipleTypesPresentIncludingRegular() throws JsonParseException, JsonMappingException, IOException {
//First upsert executed (i.e. with positional parameter) and existing document has been updated.
Assert.isTrue(queryOperations.updateDepartmentCollectionWithoutFind("Jo", "2014-09-29T04:00:00.000Z"));
}