如何使用moongoose在mongodb中查找所有文档

时间:2016-10-31 11:47:22

标签: javascript node.js mongodb mongoose

这是我的查询以获取医生集合,

这是我的id数组

var getData = ['123431243124', '13412342314321']

 dbModel.user.find({
        'tags' : {$all:getData }
    }, function(err, data) {
        if (!err) {
            if (data == null) {
                res.status(202).json({
                    "success": "0",
                    "message": "User not found"
                });
            } else {
                console.log(data)
            }
        } else {
            res.status(200).json({
                "success": "0",
                "message": err
            });
        }
    });

我在控制台中获取空数组。

我正在做的错误是什么?我怎么能得到它?

2 个答案:

答案 0 :(得分:2)

尝试使用$in代替$all

例如

Model.find({'tags' : {$in:getData }}, function(err, data) {
      if(err){
       res.status(200).json({
            "success": "0",
            "message": err
        });
     }else{
         if (data == null) {
            res.status(202).json({
                "success": "0",
                "message": "User not found"
            });
        } else {
            console.log(data)
        }
      }
     });

答案 1 :(得分:0)

You must first create the DoctorSchema of your document after that create the model and from your model you can do your query

var doctorSchema = new mongoose.Schema({})
var Doctor = mongoose.model("doctor",doctorSchema)

var getData = ['123431243124', '13412342314321']

Doctor.find({tags: {$all: getData }},function(err,datas){
    if(err){
        console.log(err)
    } else {
        // use your data here 
    }
})