3

MongoDB Schema that I have :

const userTaskSchema = new mongoose.Schema(
    {
       {...}{..}{...}{..} //Multiple objs above here, I just need help with this userTasks

        userTasks: [
            // Array of tasks with date
            {
                task: {
                    type: String,
                    required: true,
                },
                date: {
                    type: Date,
                    default: Date.now,
                },
                done: {
                    type: Boolean,
                    default: false,
                },
            },
        ],
    },

The array of task looks like this : Current output

"userTasks": [
                    {
                        "done": true,
                        "_id": "6059c57339d5590b04cb0fec",
                        "task": "abc",
                        "date": "2021-03-23T10:39:47.881Z"
                    },
                    {
                        "done": false,
                        "_id": "6059c57739d5590b04cb0fed",
                        "task": "abc",
                        "date": "2021-03-23T10:39:51.558Z"
                    },
                    {
                        "done": false,
                        "_id": "6059c57839d5590b04cb0fee",
                        "task": "abc",
                        "date": "2021-03-23T10:39:52.528Z"
                    },
                    {
                        "done": false,
                        "_id": "6059c57939d5590b04cb0fef",
                        "task": "abc",
                        "date": "2021-03-23T10:39:53.497Z"
                    },
                    {
                        "done": true,
                        "_id": "6059c57a39d5590b04cb0ff0",
                        "task": "abc",
                        "date": "2021-03-23T10:39:54.323Z"
                    }
                ],

My desired output is the array which has only done : false values present, I do not want any done : true

I cannot change the schema, this is what I have to work with.

My desired Output :

"userTasks": [
                    {
                        "done": false,
                        "_id": "6059c57739d5590b04cb0fed",
                        "task": "abc",
                        "date": "2021-03-23T10:39:51.558Z"
                    },
                    {
                        "done": false,
                        "_id": "6059c57839d5590b04cb0fee",
                        "task": "abc",
                        "date": "2021-03-23T10:39:52.528Z"
                    },
                    {
                        "done": false,
                        "_id": "6059c57939d5590b04cb0fef",
                        "task": "abc",
                        "date": "2021-03-23T10:39:53.497Z"
                    },
                    ],

I am unable to find any query to get the desired result. Please Help.

1
  • 1
    UserTask is actually around 150-500+ objects array and have a lot of true and false randomly. Commented Mar 23, 2021 at 14:10

1 Answer 1

2

Demo - https://mongoplayground.net/p/GBF1aWVu0k7

Use $filter in aggregation query

$project

db.collection.aggregate({
  "$project": {
    a: 1,
    "userTasks": {
      $filter: {
        input: "$userTasks",
        as: "task",
        cond: { "$eq": [ "$$task.done", true] //this is working for me
        }
      }
    }
  }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.