MongoDB Cheat Sheet
MongoDB Cheat Sheet
Ограниченный массив данных
Модификатор $slice в команде $push
позволяет ограничить размер обновляемого массива:
$push: { "field": { $each: ["val1", "val2"], $slice: -10 }}
Локальный Replica Set
# docker-compose.yml
version: "3.8"
services:
mongo1:
image: mongo:6.0.13
command: ["--replSet", "rs0", "--bind_ip_all"]
ports:
- 27017:27017
healthcheck:
test: echo "try { rs.status() } catch (err) { rs.initiate({_id:'rs0',members:[{_id:0,host:'localhost:27017',priority:1}]}) }" | mongosh --port 27017 --quiet
interval: 5s
timeout: 30s
start_period: 0s
start_interval: 1s
retries: 30
GROUP BY COUNT
db.getCollection('device-event-entity').aggregate(
[
{
"$group": {
_id: "$aggregateId",
count: { $sum: 1 }
}
}
]
)