WeChat Public Number: An Outstanding Disabled Person. If you have any questions, please leave a message backstage. I won’t listen anyway. Preface For example, today I will introduce how SpringBoot integrates MongoDB. Introduction to MongoDB MongoDB is a non-relational database written by C++. It is an open source database system based on distributed file storage. ..
Tag : mongodb
1. Function introduction Users can complete registration, login and browse the goods after login. After logging in, the user can purchase and add related products to the shopping cart. Users can add, reduce and delete the goods in the shopping cart. Users can settle the shopping cart goods. 2. Technical Selection This project involves the ..
mongodb docker mongodb: image: daocloud.io/library/mongo:latest ports: – “27017:27017” robomongo(mongo mac client) View data springboot Pom dependency <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> application.yml spring: data: mongodb: host: mongodb port: 27017 database: docker-demo-java-mongo repositories: enabled: true domain import org.springframework.data.annotation.Id; import java.util.Date; /** * Created by codecraft on 2016-05-20. */ public class Visitor { @Id String id; String ip; ..
Example public long countBetweenFields(String userId){ Criteria criteria = new Criteria() { @Override public DBObject getCriteriaObject() { DBObject obj = new BasicDBObject(); obj.put(“$where”, “this.lastUpdateTime > this.lastReadTime”); return obj; } }; Query query = new Query(); query.addCriteria(Criteria.where(“userId”).is(userId)); query.addCriteria(criteria); return mongoTemplate.count(query,”forum”); } docs how-to-compare-2-fields-in-spring-data-mongodb-using-query-object mongodb-spring-data-comparison-between-fields spring-data-mongodb-compare-two-docu..
Org public class Org { @Id private String id; private String name; private Set<Employee> employees; 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 Set<Employee> getEmployees() { return employees; } public ..
Define self-increasing collection @Document public class MongoSequence { @Id private String id; private int seq; public String getId() { return id; } public void setId(String id) { this.id = id; } public int getSeq() { return seq; } public void setSeq(int seq) { this.seq = seq; } } Get self-increasing id @Autowired MongoTemplate mongo; public ..
Dense index Mongo’s indexes are intensive by default. In other words, in an indexed collection, each document will have a corresponding index item, even if there are no indexed keys in the document. For example, for a product set in an e-commerce data model, suppose you build an index on the product attribute category_ids. Now ..
Be careful when declaring indexes Because this step is too easy, it is also easy to inadvertently trigger index building. If the data set is large, the construction will take a long time. In a production environment, this is a nightmare, because there is no way to stop indexing. If this happens, you will have ..
When creating a database, MongoDB allocates a set of data files on disk, and all collections, indexes, and other metadata of the database are stored in these files. The data files are all placed in the dbpath specified when mongod is started. When dbpath is not specified, mongod saves all files in /data/db.Garden.ns is the ..
Template import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; Aggregation agg = newAggregation( pipelineOP1(), pipelineOP2(), pipelineOPn() ); AggregationResults<OutputType> results = mongoTemplate.aggregate(agg, “INPUT_COLLECTION_NAME”, OutputType.class); List<OutputType> mappedResult = results.getMappedResults(); Example List<AggregationOperation> aggs = new ArrayList<>(); aggs.add(Aggregation.match(Criteria.where(“name”).is(“log”))); aggs.add(Aggregation.group(“code”).count().as(“count”)); aggs.add(Aggregation.project() .and(“_id”).as(“code”) .and(“count”).as(“count”)); Aggregation agg = Aggregation.newAggregation(aggs); AggregationResults<Map> results = mongoTemplate.aggregate(agg,”yourdocument”, Map.class); doc Spring Data MongoDB – Reference Doc..
db.stats db.stats(); Bytes unit by defaultReturn { “db” : “xxx”, //当前数据库 “collections” : 27, //当前数据库多少表 “objects” : 18738550, //当前数据库所有表多少条数据 “avgObjSize” : 1153.54876188392, //每条数据的平均大小 “dataSize” : 21615831152.0, //所有数据的总大小 “storageSize” : 23223312272.0, //所有数据占的磁盘大小 “numExtents” : 121, “indexes” : 26, //索引数 “indexSize” : 821082976, //索引大小 “fileSize” : 25691160576.0, //预分配给数据库的文件大小 “nsSizeMB” : 16, “dataFileVersion” : { “major” : 4, ..
maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> domain @Document(collection=”coffeeShop”) public class CoffeeShop { @Id private String id; private String name; @GeoSpatialIndexed private double[] location; //…. } Near query Spherical is true, the distance unit is spatial radian, false, the distance unit is horizontal unit Degree query Spherical is false and the parameter is kilometers divided by 111. ..