Javascript Mongoose允许对不存在的字段进行排序。如何仅对已存在的字段进行限制?
下面的查询返回数据,但文档(db)中不存在hgf字段。如何限制仅对现有字段进行排序Javascript Mongoose允许对不存在的字段进行排序。如何仅对已存在的字段进行限制?,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,下面的查询返回数据,但文档(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': nu
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: {} });
谢谢@Dijkstra,这很有效!!返回空数组。但我预计会有错误。