1// find all athletes that play tennis
2var query = Athlete.find({ 'sport': 'Tennis' });
3
4// selecting the 'name' and 'age' fields
5query.select('name age');
6
7// limit our results to 5 items
8query.limit(5);
9
10// sort by age
11query.sort({ age: -1 });
12
13// execute the query at a later time
14query.exec(function (err, athletes) {
15 if (err) return handleError(err);
16 // athletes contains an ordered list of 5 athletes who play Tennis
17})
1var schema = new Schema(
2{
3 name: String,
4 binary: Buffer,
5 living: Boolean,
6 updated: { type: Date, default: Date.now() },
7 age: { type: Number, min: 18, max: 65, required: true },
8 mixed: Schema.Types.Mixed,
9 _someId: Schema.Types.ObjectId,
10 array: [],
11 ofString: [String], // You can also have an array of each of the other types too.
12 nested: { stuff: { type: String, lowercase: true, trim: true } }
13})
1//Require Mongoose
2var mongoose = require('mongoose');
3
4//Define a schema
5var Schema = mongoose.Schema;
6
7var SomeModelSchema = new Schema({
8 a_string: String,
9 a_date: Date
10});
11
1Athlete.
2 find().
3 where('sport').equals('Tennis').
4 where('age').gt(17).lt(50). //Additional where query
5 limit(5).
6 sort({ age: -1 }).
7 select('name age').
8 exec(callback); // where callback is the name of our callback function.
1// Define schema
2var Schema = mongoose.Schema;
3
4var SomeModelSchema = new Schema({
5 a_string: String,
6 a_date: Date
7});
8
9// Compile model from schema
10var SomeModel = mongoose.model('SomeModel', SomeModelSchema );