get data from mongodb using mongoose code example
Example 1: mongoose and express get requests
app.get('/api/all', (req,res) =>{
Model.find({})
.then(model=> {
res.json(model)
}).catch(error=>{
res.json(error)
}
})
})
Example 2: get data from mongodb node js using mongoose
Athlete.
find().
where('sport').equals('Tennis').
where('age').gt(17).lt(50).
limit(5).
sort({ age: -1 }).
select('name age').
exec(callback);
Example 3: get data from mongodb node js using mongoose
var schema = new Schema(
{
name: String,
binary: Buffer,
living: Boolean,
updated: { type: Date, default: Date.now() },
age: { type: Number, min: 18, max: 65, required: true },
mixed: Schema.Types.Mixed,
_someId: Schema.Types.ObjectId,
array: [],
ofString: [String],
nested: { stuff: { type: String, lowercase: true, trim: true } }
})
Example 4: get data from mongodb node js using mongoose
var Schema = mongoose.Schema;
var SomeModelSchema = new Schema({
a_string: String,
a_date: Date
});
var SomeModel = mongoose.model('SomeModel', SomeModelSchema );
Example 5: get data from mongodb node js using mongoose
var query = Athlete.find({ 'sport': 'Tennis' });
query.select('name age');
query.limit(5);
query.sort({ age: -1 });
query.exec(function (err, athletes) {
if (err) return handleError(err);
})