简体   繁体   中英

How to find all documents in mongodb using moongoose

Here is my Query to fetch the doctor collection,

Here is my id array

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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