find()查询的更短的方法

我想要find提供的坐标单元格是否存在于数据库中。 有没有更简单的方法来创build此查询?

var schema = new mongoose.Schema({ coors: { x: String, y: String } }); // coors = {x:'1', y:'2'}; schema.statics.is_cell_exists = function (coors, callback){ var Cell = this; var coors_x = {}; var coors_y = {}; coors_x['coors'] = {x: coors.x}; coors_y['coors'] = {y: coors.y}; var query = { $and: [coors_x, coors_y] }; Cell.find( query ).exec(...); }; 

看来你有一些不必要的variables,等等。你可以简单地使用dot notation来进入这个“coors”对象并在其上search。

 Cell.find({ $and : [{ 'coors.x' : coors.x }, { 'coors.y' : coors.y }] })