Mongoose: how to use aggregate and find together

后端 未结 2 697
情深已故
情深已故 2021-01-30 13:37

How can I use aggregate and find together in Mongoose?
i.e I have the following schema:

const schema = new Mongoose.Schema({
  created: { type:          


        
2条回答
  •  没有蜡笔的小新
    2021-01-30 14:03

    You can use as the following:

    db.locations.aggregate([
      {$match:{"your find query"}},
      {$project:{"your desired fields"}}
    ])
    

    In the match you can do stuff like:

    {{$match:{name:"whatever"}}
    

    In the project, you can select the fields you want using numbers 0 or 1 like:

    {$project:{_id:1,created:0,name:1}}
    

    Which 0 means, do not put and 1 means put.

提交回复
热议问题