Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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
Node.js Mongoose错误-使用这些参数调用时,必须在where()之后使用elemMatch()_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js Mongoose错误-使用这些参数调用时,必须在where()之后使用elemMatch()

Node.js Mongoose错误-使用这些参数调用时,必须在where()之后使用elemMatch(),node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有以下模式: UserSchema = new Schema({ 'username': { type: String, validate: [validatePresenceOf, 'a username is required'], index: { unique: true } }, 'hashed_password': String, 'salt': String, 'locations': [LocationSchema] }); LocationSchema =

我有以下模式:

UserSchema = new Schema({
  'username': { type: String, validate: [validatePresenceOf, 'a username is required'], index: { unique: true } },
  'hashed_password': String,
  'salt': String,
  'locations': [LocationSchema]
});

LocationSchema = new Schema({
  'lat': Number,
  'lng': Number,
  'address': { type: String, validate: [validatePresenceOf, 'The address is required in order to add a new location.'] },
  'name': String
});
我试图通过id查找特定的单个位置文档。为此,我试图通过位置键(位置文档数组)查询用户集合项。我的查询如下所示:

var query = User.find({"locations.$": 1})
                .where()
                .elemMatch({locations: {_id : ObjectId('531283690315992f05bcdc98')}})
                .exec(function(err, data){

                  console.log(err);
                  console.log(data);
                });
当它运行时,我得到以下错误:

错误:使用这些参数调用时,必须在where()之后使用elemMatch()

这是什么意思?我似乎找不到一个好的解释


忘了提到,我可以通过运行以下命令从mongo shell获取所需的数据:
db.users.find({locations:{$elemMatch:{{u id:ObjectId('531283690315992f05bcdc98')}},{“locations.$”:1})

您需要提供一条路径,指向您的
,其中
调用并重新排序:

User.find()
    .where('locations')
    .elemMatch({_id : ObjectId('531283690315992f05bcdc98')})
    .select({'locations.$': 1})
    .exec(function(err, data){
        console.log(err);
        console.log(data);
    });
但您也可以简化它,因为您不需要在此处使用
$elemMatch
,您可以让Mongoose负责铸造:

User.find()
    .where('locations._id', '531283690315992f05bcdc98')
    .select({'locations.$': 1})
    .exec(function(err, data){
        console.log(err);
        console.log(data);
    });

您需要提供一条路径,指向您的
,其中
调用并重新排序:

User.find()
    .where('locations')
    .elemMatch({_id : ObjectId('531283690315992f05bcdc98')})
    .select({'locations.$': 1})
    .exec(function(err, data){
        console.log(err);
        console.log(data);
    });
但您也可以简化它,因为您不需要在此处使用
$elemMatch
,您可以让Mongoose负责铸造:

User.find()
    .where('locations._id', '531283690315992f05bcdc98')
    .select({'locations.$': 1})
    .exec(function(err, data){
        console.log(err);
        console.log(data);
    });

为什么要指定elemMatch?只有当你提供了不止一个匹配的条件时才需要它。我是mongo的新手,不知道这一点。谢谢你的提醒。你为什么要指定elemMatch?只有当你提供了不止一个匹配的条件时才需要它。我是mongo的新手,不知道这一点。谢谢你的提醒。