'Getting directly an array from a mongoose model

I'm trying to query directly to an array inside of an mongoose document, but for the moment i have couldn't.

The document example:

{
  _id:62141a799b646c7926fcfa9c
  firstname:"firstname"
  lastname:"lastname"
  username:"username"
  phone:"0000-0000000"
  email:"[email protected]"
  email_verified_at:null
  password:"$2a$10$LUATvtyPmlojHVdCkxP/QO9UUzQgOoGCW6xyx8YPUZkt5l7j6kHxK"
  remember_token:null
  deleted_at:null
  created_at:2022-02-21T23:04:25.097+00:00
  updated_at:2022-02-21T23:04:25.097+00:00
  notes: [
    "621954b8f073154099b92fca"
    "62142c426ca950e33baa1302"
  ]

I have a query that works but i think that isn't the most optimal approach, altough i could be wrong.

I want to get something like this:

[
  "621954b8f073154099b92fca"
  "62142c426ca950e33baa1302"
]

or populated:

[
    {
      _id: new ObjectId("621954b8f073154099b92fca"),
      user: new ObjectId("62141a799b646c7926fcfa9c"),
      type: new ObjectId("62076ce385b4eea8c5aeb8ba"),
      title: 'qwasdasd',
      content: '',
      created_at: 2022-02-25T22:14:16.515Z,
      updated_at: 2022-02-25T22:14:16.515Z
    },
    {
      _id: new ObjectId("62142c426ca950e33baa1302"),
      user: new ObjectId("62141a799b646c7926fcfa9c"),
      type: new ObjectId("62076ce385b4eea8c5aeb8ba"),
      title: 'qwasdasd',
      content: '',
      created_at: 2022-02-25T22:14:16.515Z,
      updated_at: 2022-02-25T22:14:16.515Z
    }
  ]

And even if i need to find an specific field, search it, but with the query:

const notes = await collection.findOne({ _id: '62141a799b646c7926fcfa9c', notes: { _id: '621954b8f073154099b92fca' } }, { 'notes.$._id': true });

i have this rersult:

{
  _id: new ObjectId("62141a799b646c7926fcfa9c"),
  notes: [
    "621954b8f073154099b92fca"
  ]
}

or populated:

{
  _id: new ObjectId("62141a799b646c7926fcfa9c"),
  notes: [
    {
      _id: new ObjectId("621954b8f073154099b92fca"),
      user: new ObjectId("62141a799b646c7926fcfa9c"),
      type: new ObjectId("62076ce385b4eea8c5aeb8ba"),
      title: 'qwasdasd',
      content: '',
      created_at: 2022-02-25T22:14:16.515Z,
      updated_at: 2022-02-25T22:14:16.515Z
    }
  ]
}

And i know that i can reach it filtering the ObjectId from user with '-_id notes.$._id' instead of { 'notes.$._id': true } and then destructuring the main object with a const { notes } resulting in this code:

const { notes } = await collection.findOne({ _id: '62141a799b646c7926fcfa9c', notes: { _id: '621954b8f073154099b92fca' } }, '-_id notes.$._id' );

But, it's the best approach to do this? Could i do it in different way being able to take advantage of findOne options like, skip, etc...?

PD: If there's some error it's because it's not the original code, i have modified it because in original code there're many abstractions.

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source