Mongo单个数据修改MongoTemplate ///

 @Resource

    protected MongoTemplate mongoTemplate = null;

    //MongoRepository 对个单个数据无法修改 报错

    @Test

    public void test() {

         UserDO userDO = new UserDO();

         userDO.setUserId("1");

         userDO.setPhone("12345611324");

         userDO.setSex(2);

         userDO.setBirthday(new Date());

         userDO.setName("hahahahahahha  ");

         userDO.setAvator("asdfasfasd");

         userDO.setGmtCreate(new Date());

         userDO.setGmtModified(new Date());

          UserDO save = userRepository.save(userDO);

          System.out.println(save);

    }

    

    /**

     * mongo单个字段修改

     * 

     * @return void

     */

    @Test

    public void edit() {

         Query query = Query.query(Criteria.where("user_id").is("1"));

         Update update = new Update();

         update.set("name", "飞洒的减肥哈市的开奖号");

         WriteResult user = mongoTemplate.updateFirst(query, update, UserDO.class);

         System.out.println(user);

    }

 

你可能感兴趣的:(Mongo单个数据修改MongoTemplate ///)