Skip to content

[Learning Note] MongoDB

Gukie edited this page Dec 5, 2017 · 10 revisions

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

refer: https://docs.atlas.mongodb.com/mongo-shell-connection/?_ga=2.43896805.1120728808.1510535298-1976534498.1510303448


MongoDB connection: mongodb://localhost:27017


MongoDB Compass - UI display


Spring boot integrate with MongoDB

refer:

@Id is not neccassary

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

Aggregation


Operators

  • $project is like the SELECT in SQL

Clone this wiki locally