简体   繁体   English

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

[英]How to find all documents in mongodb using moongoose

Here is my Query to fetch the doctor collection, 这是我的查询以获取医生集合,

Here is my id array 这是我的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
            });
        }
    });

I am getting empty array in the console. 我在控制台中得到空数组。

What is the mistake i am doing and how can i get that ? 我正在做的错误是什么,我该如何解决?

Try with $in instead of $all 尝试用$in代替$all

eg. 例如。

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)
        }
      }
     });
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 
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM