以下是我的JSON数据结构。
{
"id": "54e73250fc670bf2068e88c1",
"name": "abc",
"subCategory": [
{
"id": "54ec0c4ee4b036a9fa3767cd",
"name": "pqr",
"subCategory":[
{
"name":"abcd",
"subCategory":[
{
"name":"xyz"
"subCategory":[
{
........
}
]
}
}
]
},
{
"id": "54ec0c4ee4b036a9fa3767ce",
"name": "xyz"
},
{
"name": "abcde"
},
{
"name": "abcde"
},
{
"id": "54ec0c4be4b036a9fa3767ca",
"name": "abcde"
}
]
}
使用mongoDB和spring MVC进行添加,更新和删除操作时遇到问题。 我确实查询过 1.在子文档
下添加文档public void addGrandChildCategory(subCategory category, String parentId) {
List<subCategory> subCategory = null;
if (category.getSubCategory() == null) {
category.setSubCategory(subCategory);
}
mongoTemplate.updateFirst(new Query(Criteria.where("subCategory._id")
.is(new ObjectId(parentId))), new Update().push(
"subCategory.subCategory", category), subCategory.class);
}
2.update subDocument
public void updateSubCategory(String id, Integer index, String categoryName) {
mongoTemplate.updateFirst(
new Query(Criteria.where("subCategory[" + index + "].id").is(
new ObjectId(id))),
new Update().set("subCategory.name", categoryName),
subCategory.class);
}
我的模型类是:
1-类别
package com.cheasyy.cat;
import java.util.List;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* Model class for category
*
* @author Gaurav
*
*/
@JsonAutoDetect(getterVisibility = Visibility.NONE, fieldVisibility = Visibility.ANY)
@JsonSerialize(include = Inclusion.NON_NULL)
@Document(collection = "Category")
public class CategoryStructure {
private String id;
private String name;
private List<subCategory> subCategory;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<subCategory> getSubCategory() {
return subCategory;
}
public void setSubCategory(List<subCategory> subCategory) {
this.subCategory = subCategory;
}
}
2-子类别
package com.cheasyy.cat;
import java.util.List;
import org.bson.types.ObjectId;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.data.mongodb.core.mapping.Document;
@JsonAutoDetect(getterVisibility = Visibility.NONE, fieldVisibility = Visibility.ANY)
@JsonSerialize(include = Inclusion.NON_NULL)
@Document(collection = "Category")
/**
* Model class for sub Category
* @author Gaurav
*
*/
public class subCategory
{
private String id=new ObjectId().toString();
private String name;
private List<subCategory> subCategory;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<subCategory> getSubCategory() {
return subCategory;
}
public void setSubCategory(List<subCategory> subCategory) {
this.subCategory = subCategory;
}
}