mongodb更新操作

jaxagkaj  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(528)

我试图用mongo模板和micronaut 2.2.1更新这个集合,但是得到的错误是

Unknown modifier: $and. Expected a valid update modifier or pipeline-style update specified as an array

查询

Sending command '{"update": "product", "ordered": true, "$db": "FeteBird-Product", "lsid": {"id": {"$binary": {"base64": "LeLwrFoLTtKIs3IMcy361Q==", "subType": "04"}}}, "updates": [{"q": {"_id": {"$oid": "5fd23ddc65b27f5290fd245b"}}, "u": {"$and": [{"name": "123123"}, {"price": "222211111"}, {"description": "222211111"}]}}]}' with request id 8 to database FeteBird-Product on connection [connectionId{localValue:3, serverValue:93}] to server localhost:27017

更新代码

Product product = new Product(new ObjectId(model.getId()), model.getName(), model.getPrice(), model.getDescription());
            ProductSearchCriteria criteria = new ProductSearchCriteria();
            criteria.setId(model.getId());
            List<Bson> query = QueryBuilder.QueryBuilder(product);
            Bson queryId = QueryBuilder.QueryBuilder(criteria).get(0);
            UpdateResult updateResult = Single.fromPublisher(this.repository.getCollection(ProductConstrants.PRODUCT_COLLECTION_NAME, Product.class)
                    .updateOne(queryId, Filters.and(query))).blockingGet();

查询生成器

public static <T> List<Bson> QueryBuilder(T model) {
        List<Bson> filters = new ArrayList<>();
        final BeanIntrospection<Product> introspection = BeanIntrospection.getIntrospection(Product.class);
        var product = introspection.getPropertyNames();

        if (model instanceof Product) {
            Product productDb = (Product) model;
            if (productDb.getName() != null) {
                filters.add(Filters.eq(product[1], productDb.getName()));
            }
            if (productDb.getDescription() != null) {
                filters.add(Filters.eq(product[2], productDb.getDescription()));
            }
            filters.add(Filters.eq(product[3], productDb.getDescription()));
        }
        return filters;
    }

它正在添加$,在查询中使用micronaut应用程序中的mongo模板更新集合的最佳方法是什么

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题