【问题标题】:node-mongo-native and mongo-shell return different results given the same query给定相同的查询,node-mongo-native 和 mongo-shell 返回不同的结果
【发布时间】:2017-07-19 16:29:03
【问题描述】:

给定下一个代码:

  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

如果我对 mongo-shell 执行相同的查询,它会返回一个文档:

即:

> 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" : "alalalalala@lalalalal.com" }, "__v" : 0, "deleted" : false, "answered" : true, "firstAnswerTime" : 15614529, "updated" : "2016-02-01T17:28:34.868Z"

为什么我在从 node-mongo-native 启动的查询中没有得到任何结果?

我的 node-mongo-native 版本是 2.2.24。

我已经开始使用 mongoose,但切换到 node-mongo-native 来进行此查询,因为我认为这是 mongoose 的问题。顺便说一句,如果有助于澄清为什么它不起作用,我将发布我的架构:

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);

【问题讨论】:

  • 我已经使用猫鼬定义了我的模型。我要更新问题。
  • updated 字段是一个字符串,在您的应用程序中,您将其作为日期查询,这不会产生任何结果,但是当您在 mongo shell 中使用字符串查询它时,它会正确返回文档。
  • 看到updated 字段是一个字符串,您是否尝试将其作为一个字符串进行查询,即将变量更改为var dateOlder = moment().subtract(daysToBeOld, 'days').toISOString()
  • 谢谢@chridam!就是你说的日期格式。非常感谢 :) 如果您作为答案提交,我将接受作为已接受的答案。

标签: node.js mongodb mongoose mongodb-query


【解决方案1】:

在基础集合中,updated 字段存储为字符串,在您的应用程序中,您将其作为日期查询,这不会产生任何结果,但是当您在 mongo shell 中使用字符串查询它时,它会正确返回文件。

看到updated 字段是一个字符串,尝试将其作为一个字符串进行查询,即将查询变量更改为

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

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2016-07-12
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多