Mongoose Filter & Sort Array of Objects Using Aggregate

Tozzen

i'm new in Nodejs and Mongodb. I have schema like this:

var x= new Schema({
        title: string,
        articles: [{
            article_date: Date    
            seq: Number
        }]
    }),
Book = mongoose.model('Book', orderSchema);

I have data like this:

{
  title: "a",
  articles: [
    {article_dt: "2023-05-09T00:00:00.000+00:00", seq:1},
    {article_dt: "2021-12-31T00:00:00.000+00:00", seq:2},
    {article_dt: "2022-12-31T00:00:00.000+00:00", seq:3}
  ]
}

I want to show the data based on parameter article_date and sort article_date desc.

I've tried this and i can filter the data based on parameter

let paramDate: Date
Book.aggregate([{
    $project:{
        articles:{
            $filter:{
                input: '$articles',
                    as: 'art',
                        cond: {$lt: ['$$art.article_dt', paramDate]}
            }
        }
    }
}])

But i dont know how to sort the array. I want to sort desc. I've tried this https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortArray/ but it wont work..

I'm using: Mongoose 6.11.1 Node 16.16.0 Mongodb 5.0.9

Please help

Lin Du
db.collection.aggregate([
  {
    $project: {
      articles: {
        $filter: {
          input: "$articles",
          as: "art",
          cond: {
            $lt: [
              "$$art.article_dt",
              "2023-01-01T00:00:00.000+00:00"
            ]
          }
        }
      }
    }
  },
  {
    "$unwind": "$articles"
  },
  {
    "$sort": {
      "articles.article_dt": 1
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "articles": {
        "$push": {
          article_dt: "$articles.article_dt",
          seq: "$articles.seq"
        }
      }
    }
  }
])

mongoplayground

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mongoose: how to filter an array of objects inside an object

How to filter and sort an array of objects based on another array

mongoose aggregate model based on filter

Using Mongoose / MongoDB $addToSet functionality on array of objects

Using filter in a nested array of objects

Filter an array of objects using an Observable filter

Aggregate an array of objects in JavaScript

Mongoose aggregate sort and limit on a lookup

Using Filter on Array of Objects & Div's for Objects

How can I paginate a document array in mongoose using aggregate?

Mongoose: filter data if value exists in array within array of objects

Sort fixed array of objects by using another array

Javascript sort array of objects using array of priority

mongoose aggregate on a nested array

Using a selection sort to sort an array of objects?

Using find in a array of objects Schema using Mongoose

Aggregate an array in a document in mongoose

Filter array of objects using regex

aggregate nested array of objects using mongoose

mongoose: how to sort with aggregate search

append object to array of objects in mongoDB using mongoose

sort an array of objects on multiple key using array-sort

Filter mongoose array with an array?

How can I filter query subdocuments using aggregate in Mongoose?

How to filter an array of objects to remove elements based on condition in MongoDB aggregate?

Filter out array using another array of objects

MongoDB How to sort an array of objects without using aggregate & group?

Mongoose how to sort by date which is in array of objects of a field

mongoose aggregate query to filter and sort data