spring boot 使用 mongodb

https://yq.aliyun.com/articles/134279#

http://www.yiibai.com/mongodb/mongodb_create_database.html

http://blog.csdn.net/yu757371316/article/details/55210536

https://www.cnblogs.com/chenlq/p/6515876.html

 

其他

http://blog.csdn.net/congcong68/article/details/47357203

http://blog.csdn.net/u012150370/article/details/518227

 

解决方案

http://xiaofancn.iteye.com/blog/1163200

http://xiaoqiangge.com/aritcle/1501040086286.html

 

/**
* @param req
* @param res
* @return
*/
@ResponseBody
@GetMapping(value = "/article")
public Integer createUser(HttpServletRequest req, HttpServletResponse res, Article record) throws RestfulAPIException {
    try {
        List<String> tags = new LinkedList<>();
        for(int i = 0;i < 20;i++) {
            tags.add("tagname" + i);
        }

        mongoTemplate.save(new Tag("2015265", "张三", tags.toArray(new String[tags.size()])));
        //mongoTemplate. updateMulti (new Query(Criteria.where("id").is("2015265")), new Update().set("date", "2015-08-08"), "tag");

        return 0;
    } catch (BusinessException e) {
        throw new RestfulAPIException(e.getCode(), e.getMsg());
    } catch (Exception e) {
        throw new RestfulAPIException(ResponseCode.MISSING_REQUIRED_PARAMS.getCode(), e.getClass() + " : " + e.getMessage());
    }
}

/**
 * @param req
 * @param res
 * @return
 */
@ResponseBody
@GetMapping(value = "/update")
public Integer updateTag(HttpServletRequest req, HttpServletResponse res, Article record) throws RestfulAPIException {
    try {

        List<String> tags = new LinkedList<>();
        for(int i = 0;i < 20;i++) {
            tags.add("tagname" + i);
        }

        //mongoTemplate. updateMulti (new Query(Criteria.where("name").is("张三")), new Update().set("date", "2015-08-08"), "tag");
        //mongoTemplate. updateMulti (new Query(Criteria.where("name").is("张三")), new Update().set("tag2", tags.toArray(new String[tags.size()])), "tag");
        //mongoTemplate. updateMulti (new Query(Criteria.where("name").is("张三")), new Update().set("tag2.0", "总监级别"), "tag");
        mongoTemplate. updateMulti (new Query(Criteria.where("name").is("张三")), new Update().push("tag2", "总监级别"), "tag");

        return 0;
    } catch (BusinessException e) {
        throw new RestfulAPIException(e.getCode(), e.getMsg());
    } catch (Exception e) {
        throw new RestfulAPIException(ResponseCode.MISSING_REQUIRED_PARAMS.getCode(), e.getClass() + " : " + e.getMessage());
    }
}
#
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017
spring.data.mongodb.database=kth
package key.to.happiness.model;

import org.springframework.data.mongodb.core.mapping.Document;

import javax.persistence.Entity;
import java.io.Serializable;

@Entity
@Document(collection = "tag")
public class Tag implements Serializable {
    private String id;
    private String name;
    private String sex;
    private Integer age;
    private String[] tags;

    @Override
    public String toString() {
        return "tag [name=" + name + ", sex=" + sex + ", age=" + age + "]";
    }

    public Tag(String id, String name, String[] tags) {
        super();
        this.id = id;
        this.name = name;
        this.tags = tags;
    }
}

转载于:https://my.oschina.net/u/1538767/blog/1581579

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值