Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose允许对不存在的字段进行排序。如何仅对已存在的字段进行限制?_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Mongoose允许对不存在的字段进行排序。如何仅对已存在的字段进行限制?

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

下面的查询返回数据,但文档(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: {} });

谢谢@Dijkstra,这很有效!!返回空数组。但我预计会有错误。