简体   繁体   中英

deep find() query in Mongoose

so my schema looks like something like this

var PagesSchema = new mongoose.Schema({
        citiesList:{
            country:String,
            city:String
        }
});

i want to access citiesList in my route so i tried

app.get("/", function(req,res){


    Pages.find({"citiesList"}, function(err,citiesList){
    if(err){
        console.log(err);
    }else{
        res.render('landing',{citiesList:citiesList});
    }
});
});

but it's not working any advice please ?

That query is searching for any document that look like {citiesList: "citiesList"} . If you want all "citiesList", you can instead use {} to find all documents, and a projection to limit it to the citiesList field: Pages.find({}, "citiesList", cb)

https://mongoosejs.com/docs/api.html#model_Model.find

If instead you want only those subdocuments in a formatted list, you can use an aggregation to process them

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