下面的查询返回数据,但document(db)中不存在hgf字段。如何限制仅对现有字段进行排序?

files.find({ account_id: 1, deleted_at: { '$eq': null },
  status: { '$ne': 3 }}, { skip: 0, limit: 100, sort: { hgf: -1 }, projection: {} });

最佳答案

这应该工作:

files.find({
  account_id: 1,
  deleted_at: { '$eq': null },
  status: { '$ne': 3 },
  hgf: { $exists: true, $ne: null } // Check it exists and not null
}, { skip: 0, limit: 100, sort: { hgf: -1 }, projection: {} });

09-25 18:18
查看更多