-
Notifications
You must be signed in to change notification settings - Fork 0
[Learning Note] MongoDB
mongod 是一个数据交互的一个后台进程: mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations
mongos是一个分布式的路由服务: mongos for “MongoDB Shard,” is a routing service for MongoDB shard configurations that processes queries from the application layer, and determines the location of this data in the sharded cluster, in order to complete these operations
start mongodb: mongod --config /etc/mongod.conf
config file can be referred from: https://github.com/mongodb/mongo/blob/master/rpm/mongod.conf
mongodb.exe -- 启动 mongodb mongo.exe -- connection to MongoDB
connect with Mongo Shell use following command to connect to cluster by replacing with your password:
mongo "mongodb://cluster0-shard-00-00-wpy0t.mongodb.net:27017,cluster0-shard-00-01-wpy0t.mongodb.net:27017,cluster0-shard-00-02-wpy0t.mongodb.net:27017/test?replicaSet=Cluster0-shard-0" --authenticationDatabase admin --ssl --username admin --password
MongoDB connection: mongodb://localhost:27017
MongoDB Compass - UI display
Spring boot integrate with MongoDB
refer:
- https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-mongodb
- https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html
- https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongodb
- https://docs.spring.io/spring-data/data-document/docs/current/reference/html (Spring MongoDB doc)
Mongo requires that you have an '_id' field for all documents. If you don't provide one the driver will assign a ObjectId with a generated value. When using the MongoMappingConverter there are certain rules that govern how properties from the Java class is mapped to this '_id' field.
Usually, we add @Id in the Document POJO like following:
public class BaseDO {
@Id
private String id;
private Date gmtCreated;
private Date gmtModified;
The id property will be regarded as the "_id" field in MongoDB Doucument:
_id:"BD15106266516932"
_class:"org.data.model.db.BuildingDO"
name:"junit test"
plate:"no"
location:"yuhang"
if not defined @Id, following rule will be obtained:
The following outlines what property will be mapped to the '_id' document field:
- A property or field annotated with @Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field.
- A property or field without an annotation but named id will be mapped to the '_id' field.
refer: https://docs.spring.io/spring-data/data-document/docs/current/reference/html/#d0e1468
- https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.query.fluent-template-api
- https://docs.mongodb.com/manual/reference/operator/aggregation/group/
- $project is like the SELECT in SQL