[Mongoose] How does Aggregate query and return subscripts

  mongodb, question

Enquiries are as follows:

Modal.aggregate([
 { $match: {_id: 'xxx'}}
 ])

Query results:

[
 {
 name: 'n1',
 sex: 1
 },
 {
 name: 'n2',
 sex: 2
 },
 {
 name: 'n3',
 sex: 1
 }
 ]

How toaggregateQuery the current object index and return it tonFields, as follows:

[
 {
 n: 0,
 name: 'n1',
 sex: 1
 },
 {
 n: 1,
 name: 'n2',
 sex: 2
 },
 {
 n: 2,
 name: 'n3',
 sex: 1
 }
 ]

Seek a great god’s solution

First of all, your match is _id. under normal circumstances, you should only query one document. how can you return multiple documents?

Assuming that your _id is non-unique, you want to add an index attribute to each document, which is also a very strange idea, because if there are any subsequent aggregation operations, the document order will always change, adding index is completely unnecessary; If there is no subsequent aggregation, the returned document will be processed once.mapJust add it manually