Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MongoDB

Bỏ qua tài liệu trong MongoDB với skip() trong Aggregation

Để bỏ qua tài liệu trong aggregation pipeline, sử dụng stage $skip.

Tạo collection với _id số:

> db.demo263.insertOne({_id:100});
{ "acknowledged" : true, "insertedId" : 100 }
> db.demo263.insertOne({_id:200});
{ "acknowledged" : true, "insertedId" : 200 }
> db.demo263.insertOne({_id:300});
{ "acknowledged" : true, "insertedId" : 300 }

Hiển thị dữ liệu:

> db.demo263.find();

Pipeline phức tạp: $project tạo mảng ảo, $unwind, $sort, $skip 2, $limit 2:

> result = db.demo263.aggregate([
...   { $project: { v_id: { $ifNull: [null, [100, 200]] } } },
...   { $unwind: '$v_id' },
...   { $sort: { v_id: 1, _id: 1 } },
...   { $skip: 2 },
...   { $limit: 2 }
...]);

Kết quả (logic ví dụ minh họa $skip trong pipeline):