在猫鼬nodeJs中获取子数组的ID列表

Get list of ids of Subarray in mongoose nodeJs

如何从通知中获取仅通知 ID 的列表? notices 对象是 college 的子对象。 有一堆 colleges.from 我想要一所大学的通知 ID 列表。

[
    {
        "_id": "6221b844b29fdd64c3e59045",
        "admin": "6217bf38ba7d58a6fce43d05",
        "name": "K S School of information technology",
        "email": "ks@gmail.com",
        "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
    }
]

要仅获取 Id,您必须首先使您的函数异步,然后在该异步函数内部您必须使用等待,而在模型中您必须应用过滤器 属性,您可以在 Model.findById().

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

mongodb中的投影一般有2个选项

  1. $project 聚合 - 为此,您必须在管道中添加一个项目阶段以及其他阶段(如果有的话)。
db.college.aggregate([
  {
    '$project': {
      'notices._id': 1
    }
  }
])

  1. 添加 project in find 选项 - 如果您有简单的查询或没有查询,最好只在查找中使用项目
 db.college.find({your query}, {'notices._id':1})