APPENDIX · P3 · MONGODB · FOR SLIDE 10
← Back to Slide 10
P3. An array with a numerical boundary
db.player_feeds.updateOne({ _id: 1042 },
{ $set: { recent_feed: [] } })
db.player_feeds.updateOne({ _id: 1042 }, { $push: { recent_feed: {
$each: [{ seq: 1, detail: "m" }, { seq: 2, detail: "m" },
{ seq: 3, detail: "m" }, { seq: 4, detail: "m" },
{ seq: 5, detail: "m" }, { seq: 6, detail: "m" },
{ seq: 7, detail: "m" }, { seq: 8, detail: "m" }],
$slice: -5 } } })
db.player_feeds.aggregate([{ $project: {
seqs: "$recent_feed.seq", bytes: { $bsonSize: "$$ROOT" } } }])
db.player_feeds.updateOne({ _id: 1042 },
{ $push: { recent_feed: { seq: 9, detail: "m" } } })
What the result should be
matchedCount: 1
modifiedCount: 1
[ { _id: 1042,
seqs: [ 4, 5, 6, 7, 8 ],
bytes: … } ]
Document failed validation // 6-й элемент
// maxItems: 5 без $slice — отказ
$slice: -5
maxItems: 5
$bsonSize
Eight events; five remain, listed in the order they were added.
The validator rejects the entry if the record path omits $slice.
Measures the BSON of a document
A boundary is a number and a mechanism on all write paths. A complete history is a separate collection with a `player_id` and an index of { `player_id`: 1, `at`: -1 }.