Get list of ids of Subarray in mongoose nodeJs

How can I get list of only notice ids from the notices? The notices object is the sub-object of the college.
there are bunch of colleges.from there i want list of notice ids of one college.

[
    {
        "_id": "6221b844b29fdd64c3e59045",
        "admin": "6217bf38ba7d58a6fce43d05",
        "name": "K S School of information technology",
        "email": "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="204b5360474d41494c0e434f4d">[email protected]</a>",
        "phoneNo": "9283483",
        "address": "gujarat university, navrangpura",
        "city": "Ahmedabad",
        "state": "Gujarat",
        "zip": 132323,
        "followers": 34,
        "logo": "https://picsum.photos/500/300?img=1",
        "imageList": [
            {
                "url": "https://picsum.photos/500/300?img=2",
                "_id": "6221b8d8b29fdd64c3e590b0"
            },
            {
                "url": "https://picsum.photos/500/300?img=3",
                "_id": "6221b8ddb29fdd64c3e590bd"
            }
        ],
        "posts": [
            {
                "_id": "6221b844b29fdd64c3e5904b",
                "likes": 23,
                "url": "https://picsum.photos/500/300?img=1",
                "likedUser": []
            },
            {
                "_id": "6221b844b29fdd64c3e5904c",
                "url": "https://picsum.photos/500/300?img=2",
                "likedUser": [
                    "6217612917ddf1610fe905e8"
                ],
                "likes": 1,
                "isLiked": false
            },
            {
                "likes": 0,
                "url": "http://res.cloudinary.com/dofftzsmf/image/upload/v1646641956/My%20Uploads/ymnht6ob9mazepmkd2d1.png",
                "caption": "I think this will work",
                "_id": "6225c3b1dfe67efa88f7e048"
            },
            {
                "url": "http://res.cloudinary.com/dofftzsmf/image/upload/v1646669123/My%20Uploads/rwtmcchpijczaslckmnv.png",
                "caption": "hjhkjfdjdsfhjkfhdjksdfh",
                "likes": 0,
                "likedUser": [],
                "_id": "62262d4cf4daaddffb02dd3f"
            }
        ],
        "notices": [
            {
                "noticeTitle": "New announcements",
                "notice": [
                    {
                        "description": "New Notice ",
                        "noticeLink": "https://picsum.photos/500/300?img=1",
                        "_id": "6225d54895c2d6278941c421"
                    }
                ],
                "_id": "6225d54895c2d6278941c420"
            },
            {
                "noticeTitle": "New announcements",
                "notice": [
                    {
                        "description": "New Notice ",
                        "noticeLink": "https://picsum.photos/500/300?img=1",
                        "_id": "6225e49c95c2d6278941caf4"
                    }
                ],
                "_id": "6225e49c95c2d6278941caf3"
            }
        ],
        "aboutUs": [
            {
                "imageUrl": "http://res.cloudinary.com/dofftzsmf/image/upload/v1646652965/My%20Uploads/zcbihjc5yijjuqhfrmpc.png",
                "title": "New Title (Updated!!)",
                "description": "Now it is working Hello World",
                "_id": "6221b844b29fdd64c3e59050"
            },
            {
                "imageUrl": "https://picsum.photos/500/300?img=1",
                "title": "Placement",
                "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
                "_id": "6221b844b29fdd64c3e59051"
            }
        ],
        "__v": 123
    }
]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

projection in mongodb generally has 2 options

  1. $project in aggregation – for this you have to add a project stage in the pipeline along with other stages if any .
db.college.aggregate([
  {
    '$project': {
      'notices._id': 1
    }
  }
])
  1. adding project in find options – if you have simple query or no query it will be best to use project in find only
 db.college.find({your query}, {'notices._id':1})

Method 2

To get only Id you have to first make your function async and than inside of that async Function you have to use await and than in Model you have to apply filter property which you can see in Model.findById().

Method 3

db.college.find({},
{
  "notices": {
     "notice": {
        "_id": 1
      }
  }
})


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x