2017-02-28 67 views
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); 
+0

我一直在使用猫鼬定义我的模型。我要更新这个问题。 – Genar

+1

'updated'字段是一个字符串,并且在您的应用程序中,您将它作为日期进行查询,除非在mongo shell中使用字符串在文档中查询它时才会产生任何内容,否则它将正确返回文档。 – chridam

+1

看到'updated'字段是一个字符串,你试过把它作为一个查询,即将变量改变为'var dateOlder = moment()。subtract(daysToBeOld,'days')。toISOString()'? – chridam

回答

0

在底层集合,updated场被存储为一个字符串,并在您的应用程序你正在查询它作为日期,它不会产生任何东西,但是当你在mongo shell中使用字符串查询它时,它会正确地返回文档。

眼看updated场是一个字符串,尝试查询它作为一个即更改查询变量

var dateOlder = moment().subtract(daysToBeOld, 'days').toISOString();