我有下面的node.js代码,它获取学校中每个学生的会话出席情况。 但是,我需要统计表中有多少条记录,然后一次只查找和选择5000条。
const schoolName = "high_school";
var school = encodeURIComponent(schoolName);
const uri = `mongodb+srv://${username}:${password}@${host}/${school}?retryWrites=true&w=majority`;
const db = mongoose.createConnection(uri,{ useNewUrlParser: true });
const sessionAttendancesModel = db.model("session_attendances", new Schema({ name: String }));
const sessionAttendances = await sessionAttendancesModel.find().select("_id date attendance_code session student ").lean();
我知道我需要使用db.collection.countDocuments(),limit()和skip(),但不知道如何使用。
有人知道如何在我的代码中使用这个吗?
我试过:
const value = sessionAttendancesModel.find().count()
console.log(value)
但这就产生了以下问题:
Query {
_mongooseOptions: {},
_transforms: [],
_hooks: Kareem { _pres: Map {}, _posts: Map {} },
_executionCount: 0,
mongooseCollection: NativeCollection {
collection: null,
Promise: [Function: Promise],
_closed: false,
opts: {
bufferCommands: true,
capped: false,
autoCreate: undefined,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'session_attendances',
collectionName: 'session_attendances',
conn: NativeConnection {
base: [Mongoose],
collections: [Object],
models: [Object],
config: [Object],
replica: false,
options: null,
otherDbs: [],
relatedDbs: {},
states: [Object: null prototype],
_readyState: 2,
_closeCalled: false,
_hasOpened: false,
plugins: [],
id: 1,
_listening: false,
_connectionOptions: [Object],
client: [MongoClient],
'$initialConnection': [Promise],
then: [Function],
catch: [Function]
},
queue: [],
buffer: true,
emitter: EventEmitter {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
[Symbol(kCapture)]: false
}
},
model: Model { session_attendances },
schema: Schema {
obj: { name: [Function: String] },
paths: { name: [SchemaString], _id: [ObjectId], __v: [SchemaNumber] },
aliases: {},
subpaths: {},
virtuals: { id: [VirtualType] },
singleNestedPaths: {},
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
methodOptions: {},
statics: {},
tree: {
name: [Function: String],
_id: [Object],
__v: [Function: Number],
id: [VirtualType]
},
query: {},
childSchemas: [],
plugins: [ [Object], [Object], [Object], [Object], [Object] ],
'$id': 1,
s: { hooks: [Kareem] },
_userProvidedOptions: {},
options: {
typePojoToMixed: true,
typeKey: 'type',
id: true,
noVirtualId: false,
_id: true,
noId: false,
validateBeforeSave: true,
read: null,
shardKey: null,
autoIndex: null,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true
},
'$connectionPluginsApplied': true,
'$globalPluginsApplied': true
},
op: 'count',
options: {},
_conditions: {},
_fields: undefined,
_update: undefined,
_path: undefined,
_distinct: undefined,
_collection: NodeCollection {
collection: NativeCollection {
collection: null,
Promise: [Function: Promise],
_closed: false,
opts: [Object],
name: 'session_attendances',
collectionName: 'session_attendances',
conn: [NativeConnection],
queue: [],
buffer: true,
emitter: [EventEmitter]
},
collectionName: 'session_attendances'
},
_traceFunction: undefined,
'$useProjection': true
}
获取文档总数(使用猫鼬模型):
const schema = new Mongoose.Schema({ name: String });
const sessionAttendancesModel = Mongoose.model('session_attendances', schema);
const docsCount = await sessionAttendancesModel.count();
要一次选择5000个:
sessionAttendancesModel.find()
.skip(5000) // skips the first 5000 entries (skips 0 to 4999 and starts at 5000)
.limit(5000) // selects 5000 entries