2
由於下面的代碼:節點蒙戈本地和蒙戈殼返回不同的結果給出了相同的查詢
var daysToBeOld = 7;
var dateOlder = moment().subtract(daysToBeOld, 'days').toDate();
MongoClient.connect(mongouri, function(err, db) {
console.log('Filtering pending topics before: %s', dateOlder);
var conditions = {status: 'pending', updated: {$lt: dateOlder}};
console.log('Using the next filters: %j', conditions);
var topicsCol = db.collection('topics');
topicsCol.find(conditions).toArray(function (err, topics) {
if (err) {
console.log(err);
}
console.log("found %j topics", topics);
callback(err, topics);
});
});
我獲得下的console.log結果。正如你所看到的結果是一個空數組:
Filtering pending topics before: Tue Feb 21 2017 15:13:35 GMT+0000 (UTC)
Using the next filters: {"status":"pending","updated":{"$lt":"2017-02-21T15:13:35.191Z"}}
found [] topics
如果我執行對相同的查詢蒙戈殼它返回一個文件:
即:
> db.topics.find({"status":"pending","updated":{"$lt":"2017-02-21T15:13:35.191Z"}})
{ "_id" : "076bbbc0-e318-11e6-9375-e94b488c7ad8", "status" : "pending", "text" : "lalalalalla", "topicType" : "Información", "member" : "NoUsuarioForus", "service" : "Limpieza", "idCard" : "", "receiveAnswer" : "1", "toc" : "1", "date" : ISODate("2016-08-31T16:36:45Z"), "sender" : { "name" : "alalalal", "email" : "[email protected]" }, "__v" : 0, "deleted" : false, "answered" : true, "firstAnswerTime" : 15614529, "updated" : "2016-02-01T17:28:34.868Z"
爲什麼我在從node-mongo-native啓動的查詢中沒有得到任何結果?
我的node-mongo-native版本是2.2.24。
我已經開始使用貓鼬,但切換到節點mongo本機進行此查詢,因爲我認爲這是與貓鼬問題。順便說一句,我要發佈我的架構,如果它有助於解釋,爲什麼它不工作:
topic.js:
var mongoose = require('mongoose');
var mongoosePaginate = require('mongoose-paginate');
var Schema = mongoose.Schema;
var uuid = require('node-uuid');
var xss = require('xss');
var TopicSchema = new Schema({
_id: {
type: String,
default: uuid.v1
},
status: {
type: String,
// open: When the message is created first time or not exists
// pending: The issue has been answered by some employee. Waiting for answer from the customer
// closed: The issue has been resolved
enum: ['open', 'closed', 'pending']
},
sender: {
name: String,
email: {
type: String,
required: true,
}
},
text: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
},
updated: {
type: Date
},
// If the topic is answered by an user different than creator it will be true
answered: {
type: Boolean,
default: false
},
firstAnswerTime: Number,
centerId: String,
topicType: String,
member: String,
service: String,
idCard: String,
receiveAnswer: String,
toc: String,
satisfaction: Number,
deleted: {
type: Boolean,
default: false
}
});
TopicSchema.plugin(mongoosePaginate);
TopicSchema.pre('save', function(next) {
this.updated = new Date();
this.text = xss(this.text);
next();
});
module.exports = mongoose.model('Topic', TopicSchema);
我一直在使用貓鼬定義我的模型。我要更新這個問題。 – Genar
'updated'字段是一個字符串,並且在您的應用程序中,您將它作爲日期進行查詢,除非在mongo shell中使用字符串在文檔中查詢它時纔會產生任何內容,否則它將正確返回文檔。 – chridam
看到'updated'字段是一個字符串,你試過把它作爲一個查詢,即將變量改變爲'var dateOlder = moment()。subtract(daysToBeOld,'days')。toISOString()'? – chridam