"use server"

import { ArticleType } from "../data/article"
import { Link } from "../data/link"
import { getCollection } from "../mongodb";

export type SiteSearchType = {
    articles: ArticleType[];
    articleCount: number;
    links: Link[];
    linkCount: number;

}
export async function doSearch(wd: string) {
    //实现对link的搜索
    const linkCol = await getCollection('link')


    const linkList = await linkCol.aggregate([
        {
            $match: {
                $or: [ // 任意字段匹配即返回
                    { name: { $regex: wd, $options: "i" } },
                    { description: { $regex: wd, $options: "i" } },
                    { tags: { $regex: wd, $options: "i" } }
                ]
            }
        },
        {
            $addFields: {
                _id: { $toString: "$_id" }
            }
        }
    ]).toArray();
    return {
        list: linkList,
        count: linkList.length
    }
}