This is my weather model:
// Declare schema
var weatherSchema = Schema({
data: {
type: Object
},
date: {
type: String,
required: true
},
created_at: {
type: Date,
default: Date.now,
index: 1
}
});
in date
field, I store dates such as 2017-7-1, 2017-7-2, 2017-7-3, etc
Now I have many docs in this weather table, from 2017-6-20 to 2017-7-3 for instance.
So I want to retrieve the docs from 2017-7-1
to 2017-7-2
only:
Weather
.find({})
.where('date').gt('2017-7-1').lt('2017-7-2')
.exec(function (err, docs) {
console.log(docs);
if (err) {
res.set('Content-Type', 'application/json');
return res.sendStatus(-1);
}
if (!docs) {
res.set('Content-Type', 'application/json');
return res.status(200).send('Not weather data found.');
}
// return docs as json.
return res.status(200).json(docs);
});
But what I get is []
. What have I done wrong?
Any ideas?
EDIT:
The official format above does not work, but it works with this format:
var options = {
"date": {
"$gte": start,
"$lt": end
}
}
Weather
.find(options, function (err, docs) {
console.log(docs);
if (err) {
res.set('Content-Type', 'application/json');
return res.status(200).send('Error occurs: ' + err);
}
if (!docs) {
res.set('Content-Type', 'application/json');
return res.status(200).send('Not weather data found.');
}
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,DELETE');
// return docs as json.
return res.status(200).json(docs);
}).select("-created_at -_id -__v");
Why?