不能使用该部分遍历元素

时间:2017-11-15 14:34:30

标签: spring mongodb updates criteria spring-data-mongodb

我有这个问题:

 Query query = new Query();
 query.addCriteria(where("cakes.color.owner._id").is(new ObjectId(person.getId())));

 Update update = new Update();
 update.set("cakes.color.owner", person);

 mongoOperations.updateMulti(query, update, StockEntity.class);

这就是我使用的对象的结构:

    public class StockEntity {        
            private List<Cake> cakes= new ArrayList<>();
    }

    public class Cake implements Serializable {
            private Color color;
    }

    public class Color implements Serializable {
            private Seller owner;
    }

问题是我收到错误:

Write failed with error code 16837 and error message 'cannot use the part (cakes of cakes.color.owner) to traverse the element({cakes:[{cake:{color:{owner:_id:ObjectId('5a0c1bd15efad44437b2e76d')}}}]} )

为什么会出现此错误?任何想法?

谢谢!

1 个答案:

答案 0 :(得分:2)

位置运算符$ https://docs.mongodb.com/manual/reference/operator/update/positional/存在限制 我假设您使用mongo版本&lt; = 3.4

试试这个:update.set("cakes.$.color.owner", person);