0

我正在使用 MongoDB Realm,我希望能够删除重复的项目 - 你能告诉我该怎么做吗?

我尝试使用 dropDups 或 .distinct() 但仍然无法使代码正常工作。

  const {jobsPerPage = 100, page = 0} = payload.query;

  let query = {};
  if (payload.query.jobPosition) {
    query = { $text: { $search: payload.query.jobPosition } }
  } else if (payload.query.location) {
    query = { "location": { $eq: payload.query.location } }
  } else if (payload.query.companyName) {
    query = { $text: { $search: payload.query.companyName } }
  } else if (payload.query.jobDescription) {
    query = { $text: { $search: payload.query.jobDescription } }
  }
    
  const collection = context.services.get("mongodb-atlas").db("sample_jobs").collection("glassdoordbs").distinct();
  let jobsList = await collection.find(query).skip(page*jobsPerPage).limit(jobsPerPage).toArray()
  let allResults = await collection.count(query).then(num => num.toString())

  
  jobsList.forEach(job => {
    job._id = job._id.toString();
  });

  const responseData = {
    jobs: jobsList,
    page: page.toString(),
    filters: {},
    entries_per_page: jobsPerPage.toString(),
    total_results: await collection.count(query).then(num => num.toString()),
    totalPages: await Math.ceil(allResults / jobsPerPage).toString()

  };
  
  return responseData;
};```
4

0 回答 0